Javascript sort()没有正确按字母顺序排列 [英] Javascript sort() not alphabetizing properly
本文介绍了Javascript sort()没有正确按字母顺序排列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
var ccArray = [];
ccArray = $("input:checkbox[name='multiselect_ddlCC']").sort();
我可以告诉我在数组中得到了我需要的值,但是在sort()运行之后数组顺序已更改但不按字母顺序排列。我得到像ENG,ATL,NAS,CLT,BIM这样的东西。
I can tell that I get the values I need in the array, but after sort() runs the array order is changed but not alphabetically. I get something like ENG, ATL, NAS, CLT, BIM.
任何想法?
推荐答案
你必须对DOM元素进行排序并重新附加它们。
You have to sort the DOM elements and re-append them.
var $els = $("input:checkbox[name='multiselect_ddlCC']");
var $sorted = $($els.toArray().sort(function(a, b) {
return a.value > b.value;
}));
$els.replaceWith($sorted);
编辑刚刚测试 replaceWith
它不起作用,我的坏。这很好用:
Just tested replaceWith
and it didn't work, my bad. This works fine:
$els.each(function(i) {
$(this).after($sorted.eq(i));
});
演示: http://jsbin.com/agizoy/1/edit
这篇关于Javascript sort()没有正确按字母顺序排列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文