$.makeArray() converts the elements returned from the jQuery selector
to an actual array. Arrays in JavaScript have a sort method, into
which you can pass an optional sort function (by default the members
of the array are sorted lexicographically). The members of this array
are the span elements, and their text content (via .text()) is used
for the comparison.

https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array/sort


On Aug 28, 2:34 am, Audrey A Lee <audrey.lee.is...@gmail.com> wrote:
> hmm...
> okay.
>
> I think I understand this (a little-bit).
>
> It reminds me of how I sort objects in an array in Ruby.
>
> The way I do that is I need to write a method which compares object-a
> to object-b.
>
> So if each object has attributes like name, address, phone, age,
> income, I need to decide which attribute will be used in the
> comparison.
>
> That's the 1st idea.
>
> The 2nd idea is that I need to specify Ruby syntax.
>
> To me it is a 1 line black box.  But it does work and it is easy for
> me to memorize.
>
> I use a Ruby built-in-operator which contains NO LETTERS; here it is:
> <=>
>
> So the Ruby expression:
> 8 <=> 9
> returns -1
> and
> 9 <=> 8
> returns 1
>
> So my Ruby method to compare object-a to object-b might be very simple
> to write:
>
> def compare_em(obja, objb)
>   return obja.name <=> objb.name
> end
>
> Or I might write this to make use of the same operator syntax:
>
> def <=>(obja, objb)
>   return obja.name <=> objb.name
> end
>
> If I work with the above syntax so it can live in the class of obja
> and objb,
> I might have this:
>
> class Person
>   def <=>(objb)
>     return self.name <=> objb.name
>   end
> end
>
> Then, I could create a array of persons:
>
> person1.name = "Fred"
> person2.name = "Alan"
> person3.name = "Sally"
>
> persons = [person1,person2,person3]
>
> And the expression:
> persons.sorted
>
> would give me:
>
> [person2,person1,person3]
>
> Using Ruby to understand JavaScript is a strange path, but my mind
> does what it can.
>
> -Audrey
>
> On Aug 27, 4:49 pm, mkmanning <michaell...@gmail.com> wrote:
>
>
>
> > I'm gonna go out on a limb and assume you want them sorted
> > alphabetically, by span content:
>
> > var sorted = $.makeArray($('#names span')).sort(function(a,b){
> > return ($(a).text() < $(b).text()) ? -1 : 1;});
>
> > $('#names').html(sorted);
>
> > On Aug 25, 9:14 pm, Audrey Lee <audrey.lee.is...@gmail.com> wrote:
>
> > > Hello,
>
> > > Assume I have 3 span-elements:
>
> > > <div id="names">
> > >   <span id="s1">Fred</span>
> > >   <span id="s2">Alan</span>
> > >   <span id="s3">Sally</span>
> > > </div>
>
> > > Is there a jQuery way to sort them so I get this:
>
> > > <div id="names">
> > >   <span id="s2">Alan</span>
> > >   <span id="s1">Fred</span>
> > >   <span id="s3">Sally</span>
> > > </div>
>
> > > ??
>
> > > -Audrey

Reply via email to