Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Javascript对html表进行数字排序-10以上不起作用_Javascript_Sorting_Html Table - Fatal编程技术网

使用Javascript对html表进行数字排序-10以上不起作用

使用Javascript对html表进行数字排序-10以上不起作用,javascript,sorting,html-table,Javascript,Sorting,Html Table,下面的JS函数仅根据td中的第一个数字对myTable中的td:s进行排序。如何根据td中的所有数字进行排序(从低到高排序;例如1,5,15而不是1,15,5) 函数排序表(){ var表,行,切换,i,x,y,shouldSwitch; table=document.getElementById(“myTable”); 切换=真; while(切换){ 切换=错误; 行=表。行; 对于(i=1;iy.innerHTML.toLowerCase()){ shouldSwitch=true; 打破

下面的JS函数仅根据td中的第一个数字对myTable中的td:s进行排序。如何根据td中的所有数字进行排序(从低到高排序;例如1,5,15而不是1,15,5)

函数排序表(){
var表,行,切换,i,x,y,shouldSwitch;
table=document.getElementById(“myTable”);
切换=真;
while(切换){
切换=错误;
行=表。行;
对于(i=1;i<(rows.length-1);i++){
shouldSwitch=false;
x=行[i].getElementsByTagName(“TD”)[0];
y=行[i+1].getElementsByTagName(“TD”)[0];
if(x.innerHTML.toLowerCase()>y.innerHTML.toLowerCase()){
shouldSwitch=true;
打破
}
}
如果(应切换){
行[i].parentNode.insertBefore(行[i+1],行[i]);
切换=真;
}
}
}
使用
parseInt()
将字符串转换为整数

所以,比较应该这样做-

if (parseInt(x.innerHTML.toLowerCase()) > parseInt(y.innerHTML.toLowerCase())) {

对数字而不是字符串进行排序。例如,
如果(+(x.innerHTML)>+(y.innerHTML))…
您是否可以更新您的问题并包括您正在使用的表格的示例请,您是否也尝试过解析数字a
int
,而不是字符串?。非常感谢。
if (parseInt(x.innerHTML.toLowerCase()) > parseInt(y.innerHTML.toLowerCase())) {