如何在javascript中对数字进行排序

如何在javascript中对数字进行排序,javascript,Javascript,我试图在表格中使用排序。我使用下面给出的方法,但使用贝娄方法对数字进行排序时出现错误。此方法工作正常,但以数字ASC和DESC顺序出现问题。我不明白为什么会这样。伙计们,你们知道吗 function sortTable(n, selector) { var table, rows, switching, i, x, y, shouldSwitch, dir, switchcount = 0; ///table = document.getElementById(selector); table

我试图在表格中使用排序。我使用下面给出的方法,但使用贝娄方法对数字进行排序时出现错误。此方法工作正常,但以数字ASC和DESC顺序出现问题。我不明白为什么会这样。伙计们,你们知道吗

function sortTable(n, selector) {
var table, rows, switching, i, x, y, shouldSwitch, dir, switchcount = 0;
///table = document.getElementById(selector);
table = $(selector);
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).find('tr'); ///table.getElementsByTagName("TR");
    /*Loop through all table rows (except the
    first, which contains table headers):*/
    for (i = 0; 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 (x != null && y != null) {
            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(选择器);
表=$(选择器);
切换=真;
//将排序方向设置为升序:
dir=“asc”;
/*做一个循环,循环将持续到
未进行任何切换:*/
while(切换){
//首先说:未进行任何切换:
切换=错误;
rows=$(table.find('tr');///table.getElementsByTagName(“tr”);
/*循环遍历所有表行(除
首先,它包含表标题):*/
对于(i=0;i<(rows.length-1);i++){
//首先说,不应进行切换:
shouldSwitch=false;
/*获取要比较的两个元素,
一个来自当前行,另一个来自下一行:*/
x=行[i].getElementsByTagName(“TD”)[n];
y=行[i+1].getElementsByTagName(“TD”)[n];
/*检查两排是否应交换位置,
根据方向,asc或desc:*/
如果(x!=null&&y!=null){
如果(目录==“asc”){
if(x.innerHTML.toLowerCase()>y.innerHTML.toLowerCase()){
//如果是,标记为开关并断开回路:
shouldSwitch=true;
打破
}
}否则,如果(dir==“desc”){
if(x.innerHTML.toLowerCase()
如果要使用任何特定列对表行进行排序并按数字排序,可以利用内置的Array.prototype.sort方法,将要排序的行放入数组,对数组进行排序,然后按所需顺序将行放入表中。例如:

功能排序(evt){
var table=this;//单击了表
var cell=evt.target;//单击单元格
var col=cell.cellIndex;//列号
//将行放入数组中
var rowArray=[].slice.call(table.rows);
//或用于ECMAScript 2015
//var rowArray=Array.from(table.rows);
//排序行
rowArray.sort(函数(a,b){
//获取要排序的列的值
a=a.cells[col].textContent;
b=b.cells[col].textContent;
//如果是数字,则按数字排序
如果(是数字(a)){
返回a-b;
}
//此处的其他排序选项,如日期
//否则,按字符串排序
返回a.localeCompare(b);
});
//按顺序将行放回表中
var-tbody=table.tBodies[0];
rowArray.forEach(函数(行){
tbody.appendChild(世界其他地区);
})
}
//辅助函数
//检查值是否为数字,“2”返回true
函数为数字(n){
return!isNaN(parseFloat(n))和&isFinite(n);
}
//将侦听器附加到表
window.onload=函数(){
document.querySelector('table')。addEventListener('click',sortMe,false);
}
表格{
边界塌陷:塌陷;
}
运输署{
边框:1px实心#bbbbbb;
宽度:5em;
}

A3
D0
地下一层
C2
var过滤器=”;
函数myFunction(){
var输入,ul,li,a,i;
输入=document.getElementById(“myInput”);
filter=input.value.toUpperCase();
var product=object.filter(filterByID)
控制台日志(产品);
}
功能过滤器BYID(项目){
if(item.First_Name.toUpperCase().indexOf(filter)>-1){
返回true;
}
返回false;
}
变量对象=[
{
“名字”:“阿黛尔”,
},
{
“名字”:“Neeraj”,
},
{
“名字”:“尼廷”,
},
]
*{
框大小:边框框;
}
#我的输入{
背景图片:url('/css/searchicon.png');
背景位置:10px 12px;
背景重复:无重复;
宽度:100%;
字体大小:16px;
填充:12px 20px 12px 40px;
边框:1px实心#ddd;
边缘底部:12px;
}
#密尔{
列表样式类型:无;
填充:0;
保证金:0;
}
#李美儿{
边框:1px实心#ddd;
页边距顶部:-1px;/*防止双边框*/
背景色:#f6f6f6;
填充:12px;
文字装饰:无;
字号:18px;
颜色:黑色;
显示:块
}
#李美儿{
背景色:#E2;
游标:默认值;
}
#myUL li a:悬停:非(.header){
背景色:#eee;
}
我的电话簿
<代码> >而不是<代码> $(表)。查找(“TR”)< /C> >使用<代码>表>行>代码>,而不是<代码>行[i]。GETelEngsByTAGNITY(“TD”)[N] < /代码>考虑<代码>行[i] .Cys[n] < /代码>。如果没有其他东西的话,打字就少了很多。;-)这是一种冒泡式的,效率很低。考虑将要排序的行放入数组中,使用ARARY.Type类型排序,然后将行放到排序顺序中。