在Javascript中对表列进行排序功能不正常
我使用w3schools提供的以下Javascript函数对列进行排序:在Javascript中对表列进行排序功能不正常,javascript,function,sorting,html-table,Javascript,Function,Sorting,Html Table,我使用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: di
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;
}
}
}
}
函数排序表(n){
变量表,行,切换,i,x,y,shouldSwitch,dir,switchcount=0;
table=document.getElementById(“myTable”);
切换=真;
//将排序方向设置为升序:
dir=“asc”;
/*做一个循环,循环将持续到
未进行任何切换:*/
while(切换){
//首先说:未进行任何切换:
切换=错误;
行=table.getElementsByTagName(“TR”);
/*循环遍历所有表行(除
首先,它包含表标题):*/
对于(i=1;i<(rows.length-1);i++){
//首先说,不应进行切换:
shouldSwitch=false;
/*获取要比较的两个元素,
一个来自当前行,另一个来自下一行:*/
x=行[i].getElementsByTagName(“TD”)[n];
y=行[i+1].getElementsByTagName(“TD”)[n];
/*检查两排是否应交换位置,
根据方向,asc或desc:*/
如果(目录==“asc”){
if(x.innerHTML.toLowerCase()>y.innerHTML.toLowerCase()){
//如果是,标记为开关并断开回路:
shouldSwitch=true;
打破
}
}否则,如果(dir==“desc”){
if(x.innerHTML.toLowerCase()
然而,在我的名为InternetExplorer版本的专栏文章中,它并不能很好地工作
它与表中的其他列配合得很好。我不知道为什么会这样。有人能告诉我出了什么事吗
我将这些值复制到excel工作表上,并尝试对其进行排序,但排序也不正确。这是价值观的问题吗
它只是将数字的第一位数字考虑在内,然后进行排序。如何解决此问题?根据字符串运算比较规则,结果是正确的,但您的要求不同 字符串比较是如何发生的? A
/**
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;
}
/**
A和B将是数组,元素是整数
如果AB[i])
返回false;
}
if(a_长度
简而言之,例如,使用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);
函数排序表(n){
变量表,行,切换,i,x,y,shouldSwitch,dir,switchcount=0;
table=document.getElementById(“myTable”);
切换=真;
//将排序方向设置为升序:
dir=“asc”;
/*做一个循环,循环将持续到
未进行任何切换:*/
//获取用于填充的最长版本字符
var padding='000000000000';//12个字符
while(切换){
//首先说:未进行任何切换:
切换=错误;
行=table.getElementsByTagName(“TR”);
/*循环遍历所有表行(除
首先,它包含表标题):*/
对于(i=1;i<(rows.length-1);i++){
//首先说,不应进行切换:
shouldSwitch=false;
/*获取要比较的两个元素,
一个来自当前行,另一个来自下一行:*/
x=行[i].getElementsByTagName(“TD”)[n];
y=行[i+1].getElementsByTagName(“TD”)[n];
/*检查两排是否应交换位置,
根据方向,asc或desc:*/
//黑客,删除点
替换(/\d+/g,n=>+n+100000);
y=y.innerHTML.replace(/\d+/g,n=>+n+100000);
//console.log(x)
如果(目录==“asc”){
如果(x>y){
//如果是,标记为开关并断开回路:
shouldSwitch=true;
打破
}
}否则,如果(dir==“desc”){
if(x