【问题标题】:Sorting a column of table in Javascript not functioning properly在Javascript中对表格列进行排序无法正常工作
【发布时间】:2023-03-24 09:52:01
【问题描述】:

我有来自 w3schools 的以下 Javascript 函数来对列进行排序:

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.getElementsByTagName("TR");
    /*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;
      }
    }
  }
}

但是它在我的名为 Internet Explorer 版本的专栏上效果不佳。

它适用于我表的其他列。我不知道为什么会这样。谁能告诉我出了什么问题?

我将这些值复制到 Excel 工作表上并尝试对其进行排序,但排序也不正确。是价值观的问题吗?

它只是考虑数字的第一位然后排序。我该如何解决这个问题?

【问题讨论】:

  • @sauntimo 你能帮忙吗?
  • 张贴你的桌子
  • 不是图片而是html代码
  • document.write() 覆盖所有内容。不要使用它。由于您要将字符串解析为 HTML,请使用 innerHTML 甚至更好的 insertAdjacentHTML()
  • 欢迎来到 Stack Overflow!请不要破坏您的帖子。提交帖子后,您已将内容许可给整个 Stack Overflow 社区(根据 CC-by-SA 许可)。如果您想取消此帖子与您帐户的关联,请参阅What is the proper route for a disassociation request?

标签: javascript function sorting html-table


【解决方案1】:

根据字符串运算比较规则,结果是正确的,但你的要求不同。

字符串比较是如何发生的? 一个

  1. 比较两个字符串的第一个数字的UTF-16值,如果相等,然后比较下一个字符。

  2. 如果 A 字符串的某个字符小于 B,则 A 字符串会更小。

  3. 如果任何一个字符串在得出结论之前就结束了,它将被视为小字符串。

例如

  1. "10.0.9200.17609"
  2. "9"
  3. "101"

对于您的问题,您需要在 .(dot) 上拆分数字 例如拆分后的“10.0.9200.17609” [10, 0, 9200, 17609] 那么你需要使用下面的比较器函数来决定你的排序算法中哪个元素更小。

/** 
  A and B will be array and element are integer
  return true if A < B else false
**/
function comparator(A, B) {
   var a_length = A.length, b_length = B.length;
   var loop_count = min(a_length, b_length);
   for(var i = 0; i < loop_count; i++) {
     if(A[i] < B[i])
        return true;
     else if (A[i] > B[i])
        return false;
   }

   if(a_length < b_length)
     return true;

   return false;
}

【讨论】:

  • 那么,如何以及在何处将其集成到我的代码中?
  • x.innerHTML.toLowerCase()
  • JavaScript 使用 Unicode 字符集的 UTF-16 编码,而不是 ASCII。 (Java、.NET、VB6、……等等)。
  • 是的,你是对的,但 Unicode 是 ASCII 的超集,数字 0-128 在 ASCII 中的含义与在 Unicode 中的含义相同
【解决方案2】:

简而言之,使用100000 创建填充,例如

8.00.6001.18372 => 100008.100000.106001.118372
11.0.15063.0 => 100011.100000.115063.100000

使用正则表达式

x = x.innerHTML.replace(/\d+/g, n => +n + 100000);

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:*/
  // get longest version characters for padding
  var padding = '000000000000'; // 12 chars
  while (switching) {
    //start by saying: no switching is done:
    switching = false;
    rows = table.getElementsByTagName("TR");
    /*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:*/
      // the hack, remove dot      
      x = x.innerHTML.replace(/\d+/g, n => +n + 100000);
      y = y.innerHTML.replace(/\d+/g, n => +n + 100000);
     //console.log(x)
      if (dir == "asc") {
        if (x > y) {
          //if so, mark as a switch and break the loop:
          shouldSwitch = true;
          break;
        }
      } else if (dir == "desc") {
        if (x < y) {
          //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;
      }
    }
  }
}
document.querySelector('#thead').addEventListener('click', function() {
  sortTable(0);
})
table {
  border: 1px solid #dddddd;
  width: 60%;
  border-collapse: collapse;
}

th {
  background-color: #209d9d;
  cursor: pointer;
}

th:hover {
  background-color: #1c8d8d;
}

tr {
  height: 30px;
}

th,
td {
  text-align: center;
  border-collapse: collapse;
  width: 60%;
  border: 1px solid #ddd;
  align: center
}
<table id="myTable">
  <tr><th id="thead">IE Versions</th></tr>
  <tr><td>10.0.8250.00000</td></tr>
  <tr><td>10.0.8400.00000</td></tr>
  <tr><td>10.0.9200.16384</td></tr>
  <tr><td>8.00.6001.18372</td></tr>
  <tr><td>8.00.6001.18702</td></tr>
  <tr><td>8.00.7000.00000</td></tr>
  <tr><td>8.00.7600.16385</td></tr>
  <tr><td>9.0.8080.16413</td></tr>
  <tr><td>9.0.8112.16421</td></tr>
  <tr><td>10.0.9200.17609</td></tr>
  <tr><td>11.0.15063.0</td></tr>
  <tr><td>11.0.9600.18697</td></tr>
  <tr><td>8.00.6001.18241</td></tr>
</table>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-21
    • 1970-01-01
    • 1970-01-01
    • 2020-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多