Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/476.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.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 jquery sortElement:按表数据的数值排序_Javascript_Jquery_Sorting - Fatal编程技术网

Javascript jquery sortElement:按表数据的数值排序

Javascript jquery sortElement:按表数据的数值排序,javascript,jquery,sorting,Javascript,Jquery,Sorting,这是我的建议 从小提琴中可以看到,total列包含当前按文本排序的数字 Q:如何根据数值对total列进行排序 var table = $('table'); $('#facility_header, #city_header, #total') .wrapInner('<span title="sort this column"/>') .each(function(){ var th = $(this),

这是我的建议

从小提琴中可以看到,
total
列包含当前按文本排序的数字

Q:如何根据数值对
total
列进行排序

var table = $('table');

    $('#facility_header, #city_header, #total')
        .wrapInner('<span title="sort this column"/>')
        .each(function(){

            var th = $(this),
                thIndex = th.index(),
                inverse = false;

            th.click(function(){

                table.find('td').filter(function(){

                    return $(this).index() === thIndex;

                }).sortElements(function(a, b){

                    return $.text([a]) > $.text([b]) ?
                        inverse ? -1 : 1
                        : inverse ? 1 : -1;

                }, function(){

                    // parentNode is the element we want to move
                    return this.parentNode; 

                });

                inverse = !inverse;

            });

        });
var table=$('table');
$(“#设施#标题,#城市#标题,#总计”)
.wrapInner(“”)
.each(函数({
var th=$(此),
thIndex=th.index(),
逆=假;
点击(函数(){
table.find('td').filter(函数(){
返回$(this).index()==thIndex;
}).分拣元件(功能(a、b){
返回$.text([a])>$.text([b])?
相反?-1:1
:逆?1:-1;
},函数(){
//parentNode是我们要移动的元素
返回此.parentNode;
});
逆=!逆;
});
});

在比较这些值之前,必须先解析它们

.sortElements(function(a, b){
    var sa = $.text([a]);
    var sb = $.text([b]);

    var ia = parseInt(sa);
    var ib = parseInt(sb);

    if (!isNaN(ia) && !isNaN(ib)) {
        return ia > ib ? (inverse ? -1 : 1) : (inverse ? 1 : -1);
    }

    return sa > sb ? (inverse ? -1 : 1) : (inverse ? 1 : -1);
}
这是你的最新消息