表仅按字母顺序排序,不是数字排序[关闭]

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了表仅按字母顺序排序,不是数字排序[关闭]相关的知识,希望对你有一定的参考价值。

我有一个由php变量填充的表。 PHP值来自API的JSON。

我想在同一个表格中按字母和数字排序。

以下功能有效,但只能按字母顺序对其进行排序。

我可以修改此功能以进行数字排序吗?

<script>
function sortTable(n) {
  var table, rows, switching, i, x, y, shouldSwitch, dir, switchcount = 0;
  table = document.getElementById("myTable");
  switching = true;
  //Set the sorting direction to ascending:
  dir = "asc";
  /*Make a loop that will continue until
  no switching has been done:*/
  while (switching) {
    //start by saying: no switching is done:
    switching = false;
    rows = table.rows;
    /*Loop through all table rows (except the
    first, which contains table headers):*/
    for (i = 1; i < (rows.length - 1); i++) {
      //start by saying there should be no switching:
      shouldSwitch = false;
      /*Get the two elements you want to compare,
      one from current row and one from the next:*/
      x = rows[i].getElementsByTagName("TD")[n];
      y = rows[i + 1].getElementsByTagName("TD")[n];
      /*check if the two rows should switch place,
      based on the direction, asc or desc:*/
      if (dir == "asc") {
        if (x.innerhtml.toLowerCase() > y.innerHTML.toLowerCase()) {
          //if so, mark as a switch and break the loop:
          shouldSwitch= true;
          break;
        }
      } else if (dir == "desc") {
        if (x.innerHTML.toLowerCase() < y.innerHTML.toLowerCase()) {
          //if so, mark as a switch and break the loop:
          shouldSwitch = true;
          break;
        }
      }
    }
    if (shouldSwitch) {
      /*If a switch has been marked, make the switch
      and mark that a switch has been done:*/
      rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
      switching = true;
      //Each time a switch is done, increase this count by 1:
      switchcount ++;
    } else {
      /*If no switching has been done AND the direction is "asc",
      set the direction to "desc" and run the while loop again.*/
      if (switchcount == 0 && dir == "asc") {
        dir = "desc";
        switching = true;
      }
    }
  }
}
</script>
答案
function sortTable(n) {
  var table, rows, switching, i, x, y, shouldSwitch, dir, switchcount = 0;
  var regex = /$|USD|EURO|,/gi;
  var xinnerHTML="", yinnerHTML="";

  table = document.getElementById("myTable");
  switching = true;
  dir = "asc";
  while (switching) {
      switching = false;
      rows = table.getElementsByTagName("TR");
      for (i = 1; i < (rows.length - 1); i++) {
          shouldSwitch = false;
          x = rows[i].getElementsByTagName("TD")[n];
      y = rows[i + 1].getElementsByTagName("TD")[n];


      xinnerHTML = x.innerHTML.replace(regex, '');
      yinnerHTML = y.innerHTML.replace(regex, '');

                var xContent = (isNaN(xinnerHTML))
    ? (xinnerHTML.toLowerCase() === '-')
        ? 0 : xinnerHTML.toLowerCase()
    : parseFloat(xinnerHTML);
var yContent = (isNaN(yinnerHTML))
    ? (yinnerHTML.toLowerCase() === '-')
        ? 0 : yinnerHTML.toLowerCase()
    : parseFloat(yinnerHTML);
      if (dir == "asc") {
          if (xContent > yContent) {
              shouldSwitch= true;
              break;
          }
      } else if (dir == "desc") {
          if (xContent < yContent) {
              shouldSwitch= true;
              break;
          }
      }
  }
  if (shouldSwitch) {
      rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
      switching = true;
      switchcount ++;
  } else {
      if (switchcount == 0 && dir == "asc") {
          dir = "desc";
          switching = true;
      }
     }
   }
}
另一答案

我改变了方向,找到了有效的新代码。但是,如果同一单元格中有数字和字母,则不会按数字排序。我该怎么做?我将在下面粘贴新代码。

function sortTable(n) {
  var table, rows, switching, i, x, y, shouldSwitch, dir, switchcount = 0;

  table = document.getElementById("myTable");
  switching = true;
  dir = "asc";
  while (switching) {
      switching = false;
      rows = table.getElementsByTagName("TR");
      for (i = 1; i < (rows.length - 1); i++) {
          shouldSwitch = false;
          x = rows[i].getElementsByTagName("TD")[n];
      y = rows[i + 1].getElementsByTagName("TD")[n];
                var xContent = (isNaN(x.innerHTML))
    ? (x.innerHTML.toLowerCase() === '-')
        ? 0 : x.innerHTML.toLowerCase()
    : parseFloat(x.innerHTML);
var yContent = (isNaN(y.innerHTML))
    ? (y.innerHTML.toLowerCase() === '-')
        ? 0 : y.innerHTML.toLowerCase()
    : parseFloat(y.innerHTML);
      if (dir == "asc") {
          if (xContent > yContent) {
              shouldSwitch= true;
              break;
          }
      } else if (dir == "desc") {
          if (xContent < yContent) {
              shouldSwitch= true;
              break;
          }
      }
  }
  if (shouldSwitch) {
      rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
      switching = true;
      switchcount ++;
  } else {
      if (switchcount == 0 && dir == "asc") {
          dir = "desc";
          switching = true;
      }
     }
   }
}

以上是关于表仅按字母顺序排序,不是数字排序[关闭]的主要内容,如果未能解决你的问题,请参考以下文章

首先按字母顺序对对象数组进行排序,然后按数字排序

sql server 按字母顺序排序,然后是数字

MYSQL数据库字母数字混合字段排序问题

MYSQL数据库字母数字混合字段排序问题

php 按字母顺序排序数组然后按数字排序

当字符串为数字时,如何在考虑值的同时按字母顺序对字符串进行排序?