排序一组里的标签字母数字 [英] Sort a set of li tags alphanumerically

查看:88
本文介绍了排序一组里的标签字母数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在玩周围试图获取将他们的内容,但目前无果(至少没有速度/精度)排序选择里标签的功能;

I've been playing around trying to get a function that will sort a selection of li tags by their content but currently to no avail (at least no speed/accuracy);

$('.sortasc').live('click',function(){

        var liArr = Array();

        $('#licontainer').children('li').each(function(){
           liArr.push($(this).html());
        });

        liArr.sort(alphaNumSort);

        $(liArr).each(function(){
            var current = this;

            var clone = $('li').filter(function(){return($(this).html()==current);}).clone();

            $('li').filter(function(){return($(this).html()==current);}).remove();

            clone.appendTo('#tempsortbox');

        });

        $('#licontainer').html($('#tempsortbox').html());
        $('#tempsortbox').html('')

    });

这是缓慢而且在排序不是很大。 Idealy,那就来挑选基于驻留在黎内的强烈标记的内容。

It's both slow and not GREAT at sorting. Idealy, it would sort based on the content of a strong tag that resides within the li.

这里的alphaNumSort功能,如果你有兴趣(这个作品一种享受它只是蹩脚的HTML和克隆垃圾,这不是真正的工作)

Here's the alphaNumSort function if you're interested (this works a treat its just the crappy html and cloning rubbish that's not really working)

function alphaNumSort(m,n){
try{
    var cnt= 0,tem;
    var a= m.toLowerCase();
    var b= n.toLowerCase();
    if(a== b) return 0;
    var x=/^(\.)?\d/;

    var L= Math.min(a.length,b.length)+ 1;
    while(cnt< L && a.charAt(cnt)=== b.charAt(cnt) &&
    x.test(b.substring(cnt))== false && x.test(a.substring(cnt))== false) cnt++;
    a= a.substring(cnt);
    b= b.substring(cnt);

    if(x.test(a) || x.test(b)){
        if(x.test(a)== false)return (a)? 1: -1;
        else if(x.test(b)== false)return (b)? -1: 1;
        else{
            var tem= parseFloat(a)-parseFloat(b);
            if(tem!= 0) return tem;
            else tem= a.search(/[^\.\d]/);
            if(tem== -1) tem= b.search(/[^\.\d]/);
            a= a.substring(tem);
            b= b.substring(tem);
        }
    }
    if(a== b) return 0;
    else return (a >b)? 1: -1;
}
catch(er){
    return 0;
}

}

干杯

推荐答案

我不能完全确定你的 alphaNumSort 函数做什么,但一个简单的字符串比较可能就足够了

I'm not completely sure what your alphaNumSort function does, but a simple string comparison may be enough.

var li = $('#licontainer li').get();

// sort the list items based on the contents of a nested strong tag
li.sort(function(a, b) {
    a = $('strong', a).text();
    b = $('strong', b).text();

    // you may want to process the text values further here, perhaps
    // running it through $.trim, reducing whitespace sequences with
    // a regular expression, or lower- or upper-casing letters to
    // make the comparison case-insensitive.

    return (a < b) ? -1 : ((a > b) ? 1 : 0);
});

// reinsert the list items in their new order
$('#licontainer').append(li);

这应该是比你的临时列表方式相当快,因为​​它执行较少的DOM操作。使用原生的字符串比较也应该是一个公平位比你目前的排序算法快,但如果是做具体的,我已经错过了更新收益语句东西,利用它(保持preceding线)。

This should be considerably faster than your temporary list method, as it performs fewer DOM operations. The use of native string comparisons should also be a fair bit faster than your current sorting algorithm, but if it is doing something specific that I have missed update the return statement to utilize it (keeping the preceding lines).

return alphaNumSort(a, b);

如果这是的还是的太慢,你可能会通过的隐藏名单更是提高性能,操纵它之前,$ P $无法执行不必要的重绘pventing浏览器。

If this is still too slow you could likely improve performance even more by hiding the list before manipulating it, preventing the browser from performing unnecessary repaints.

var li = $('#licontainer').hide().find('li').get();

// ...

$('#licontainer').append(li).show();

这篇关于排序一组里的标签字母数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆