admin管理员组文章数量:1341392
I have an array that is holding a number of objects.
Array:
var activeMembers=[];
The DIV objects in the above array look like as follows - each was added one at a time:
<div id="mary" class="chatmember 1011"></div>
<div id="steven" class="chatmember 1051"></div>
<div id="adam" class="chatmember 1701"></div>
<div id="bob" class="chatmember 1099"></div>
<div id="peter" class="chatmember 1123"></div>
Is there a quick way to sort theses DIV objects in the array by the ID from A-Z?
thx
I have an array that is holding a number of objects.
Array:
var activeMembers=[];
The DIV objects in the above array look like as follows - each was added one at a time:
<div id="mary" class="chatmember 1011"></div>
<div id="steven" class="chatmember 1051"></div>
<div id="adam" class="chatmember 1701"></div>
<div id="bob" class="chatmember 1099"></div>
<div id="peter" class="chatmember 1123"></div>
Is there a quick way to sort theses DIV objects in the array by the ID from A-Z?
thx
Share Improve this question edited Aug 18, 2011 at 7:56 Reporter 3,9485 gold badges35 silver badges49 bronze badges asked Aug 18, 2011 at 7:47 AdamAdam 1,9716 gold badges19 silver badges17 bronze badges 2- 6 Didn't you ask this yesterday? stackoverflow./questions/7092266/ordering-divs-from-a-z – andyb Commented Aug 18, 2011 at 7:51
- 2 @Shervin maybe he just wondered where his question went ... and reasked it ... he's quite new to so, so give him a chance :) – user57508 Commented Aug 18, 2011 at 8:03
5 Answers
Reset to default 10Since there are so many silly implementations being proposed that are using jQuery when it is only a waste of resources, I'll propose my own. This is just a straight javascript sort of an array by a property of the object in the array. To do that you just use the array sort method with a custom parison function that does an alpha parison of the id value. There is no reason or advantage to involve jQuery in this at all.
activeMembers.sort(function(a, b) {
var aID = a.id;
var bID = b.id;
return (aID == bID) ? 0 : (aID > bID) ? 1 : -1;
});
Note, as requested in the question, this sorts a list of div references in the array. It does not sort the objects in the layout of the page. To do that, you'd have to sort the references list, then rearrange the divs in the page according to the new array order.
If you can rely on the fact that no two IDs are ever the same in your own HTML (since you are never supposed to have two objects with the same ID), you can shorten and speed up the custom sort function to just be this:
activeMembers.sort(function(a, b) {
return (a.id > b.id) ? 1 : -1;
});
$('.chatmember').sort(function(a,b){ return a.id > b.id; })...
Returns (in Firebug):
[div#adam.chatmember, div#bob.chatmember, div#mary.chatmember, div#peter.chatmember, div#steven.chatmember]
Try this:
activeMembers.sort(function(a,b){
var aid = $(a).attr('id');
var bid = $(b).attr('id');
return (aid==bid) ? 0 : (aid > bid) ? 1 : -1;
});
There is no quick way. You can use generic sorter that allow you to provide parator, or you can write a custom sorter.
See here for an example.
You can use the tinysort plugin )(http://plugins.jquery./project/TinySort)
e.g.
$("#parentName > div").tsort("",{attr:"id"});
本文标签: javascriptJQUERY Sorting an Array of ObjectsStack Overflow
版权声明:本文标题:javascript - JQUERY Sorting an Array of Objects - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743595377a2507783.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论