Thanks Erik and Karl! I think for general code maintainability, I'll
keep my current solution with the for loop. It's much longer, but I
think overall it's easier to understand. I attached that code below in
case it would help anyone else later. However I'll be keeping an eye
on that jQuery sort ticket, and I'm standing in line as the second
person who cares about it :)

This code finds all <child> elements and adds their contents
(innerHTML/text) to a temporary array. The array is sorted, and I use
a for loop to print a comma-separated list of the array's values into
a <div>. I use this with an xml file source which is formatted like
this:

<element>
  <child>Text1</child>
  <child>Text2</child>
</element>

var tmp=[];
$('element').find('child').each(function(){
   tmp.push($(this).text());                                                    
                                                                                
           });
tmp = tmp.sort();
for(i=0;i<tmp.length;i++){
   $("#div_id").append(tmp[i]);
   if(i<tmp.length-1)
        $("#div_id").append(", ");
}

The result is:

<div id="div_id">Text1, Text2</div>

Jonas


On Jan 14, 6:34 pm, "Erik Beeson" <[EMAIL PROTECTED]> wrote:
> I seem to be the only person to care about sort in jQuery. This ticket has a
> sort function that worked with 1.1, though I'm not sure if it still does.
> Make sure to scroll down to the bottom for the most recent version:
>
> http://dev.jquery.com/ticket/255
>
> For example, to sort the children of an element with id "foo":
>
> $('#foo').children().sort(function(a, b) { ... }).prependTo('#foo');
>
> You have to reinsert them into the DOM after sorting them, and 'a' and 'b'
> are DOM nodes, not jQuery objects.
>
> Hope it helps.
>
> --Erik
>
> On 1/14/08, monster79 <[EMAIL PROTECTED]> wrote:
>
>
>
> > Hi all,
>
> > I'm looking for an elegant method to sort elements returned from
> > find() or an each() callback. Currently I'm dumping the elements' text
> > into a Javascript array with push(), then sorting the array
> > with .sort() and printing the results with a for() loop. This works
> > well, but it doesn't seem like the jQuery way to do it. Any
> > suggestions?
> > Jonas

Reply via email to