表格仅按字母顺序排序,而不是数字顺序

时间:2018-12-15 01:00:22

标签: javascript json sorting html-table

我有一个由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>

2 个答案:

答案 0 :(得分:0)

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;
      }
     }
   }
}

答案 1 :(得分:0)

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

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;
      }
     }
   }
}