1

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

2
  • 6
    Didn't you ask this yesterday? stackoverflow.com/questions/7092266/ordering-divs-from-a-z 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 :) Commented Aug 18, 2011 at 8:03

5 Answers 5

10

Since 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 comparison function that does an alpha comparison 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;
});
Sign up to request clarification or add additional context in comments.

Comments

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]

7 Comments

There is no sort method in jQuery. Besides, that would only have any effect if the elements are also in the page, and it would only rearrange the elements in the page, not in the array.
@Guffa Sure about that? Might want to try running it before you start guessing. The elements don't move.
@Guffa but javascript knows .sort() (javascriptkit.com/javatutors/arraysort.shtml)
@Stefan Mai: Sure about what? There doesn't exist any sort method in the documentation for jQuery: api.jquery.com/?ns0=1&s=sort
@Guffa notice to myself: drink a cup of coffee, think/request for more information and then write ... sometimes it's really hard to follow this simple guide!
|
1

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;

});

3 Comments

@downvoter could you explain your downvote to give the author a chance for elaborating/fixing his answer?
@jfriends, it stated on the tags of SO question
@jfriends, please note also that accessing of non-standard attributes with the use of obj.property may not work on all browsers, and the right way should be the obj.getAttribute(<name>) or the setAttribute. for instance if you have an <a> element that has rel attribute, you may not get the value using a.rel, using jquery .attr() handles this correctly.
0

There is no quick way. You can use generic sorter that allow you to provide comparator, or you can write a custom sorter.

See here for an example.

Comments

0

You can use the tinysort plugin )(http://plugins.jquery.com/project/TinySort)

e.g.

$("#parentName > div").tsort("",{attr:"id"});

1 Comment

That will sort elements in the page, not an array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.