用字母数字排序一组li标签(Sort a set of li tags alphanumerically)

编程入门 行业动态 更新时间:2024-10-21 17:24:06
用字母数字排序一组li标签(Sort a set of li tags alphanumerically)

我一直在玩弄试图获得一个函数,它会根据内容排序选择li标签,但目前无济于事(至少没有速度/准确性);

$('.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('') });

这在排序时既慢又不伟大。 理想情况下,它会根据驻留在li中的强标签的内容进行排序。

如果你感兴趣的话,这里是alphaNumSort函数(这个工程只是一个蹩脚的HTML和克隆垃圾,它并不是真的有效)

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; }

}

干杯

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('') });

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.

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; }

}

Cheers

最满意答案

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

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操作更少。 本地字符串比较的使用也应该比当前的排序算法快一点,但是如果它正在做一些特定的事情,我错过了更新return语句以利用它(保留前面的行)。

return alphaNumSort(a, b);

如果这仍然过于缓慢,那么在操纵它之前隐藏列表可能会进一步提高性能,从而阻止浏览器执行不必要的重新绘制。

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

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);

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);

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();

更多推荐

function,alphaNumSort,var,电脑培训,计算机培训,IT培训"/> <meta name="des

本文发布于:2023-08-05 09:51:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1431192.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:用字   标签   数字   li   alphanumerically

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!