function sortText(a, b) {
  var A = $(a).text(), B = $(b).text();
  if ( A < B ) return -1;
  else if ( A > B ) return 1;
  return 0;
}
var tgt = $('#test')
  , arr = $(tgt.find('a').get().sort(sortText))
  ;
tgt.empty().append(arr);

arr contains your (sorted) jQuery collection of links.

On Oct 22, 12:13 am, Yansky <[EMAIL PROTECTED]> wrote:
> Sorry to bump (and for the double post), but I was really hoping to
> get this working. I'm trying to sort links alphabetically by their
> text content. I'd really like to be able to hold on to the jQuery
> object element reference after sorting the links. Can anyone give me a
> hand?
>
> Cheers.
>
> On Oct 19, 2:28 am, Yansky <[EMAIL PROTECTED]> wrote:
>
> > Hi, I'm having some problems figuring out how to sort links
> > alphabetically based on their text content and store the reference to
> > the link. I can do it sans jQuery, but I'd really like to be able to
> > use jQuery to do it.
>
> > This is what I want to do:
>
> > <div id="test">
> > <a href="http://yahoo.com";>Yahoo</a>
> > <a href="http://google.com";>Google</a>
> > <a href="http://aol.com";>Aol</a>
> > </div>
>
> > function sortText(a, b) {
> >   if ( a.text < b.text ) return -1;
> >   else if ( a.text > b.text ) return 1;
> >   return 0;}
>
> > var meh=[];
>
> > var getT = document.getElementById('test');
>
> > var getas = getT.getElementsByTagName( 'a' );
> > for(var i=0;i < getas.length; i++){
> >   meh[i]={ text: getas[ i ].textContent, el: getas[ i ] };
>
> > }
>
> > var sortedLinks = meh.sort( sortText );
>
> > getT.innerHTML = '';
>
> > for(var l=0;l < sortedLinks.length; l++){
> >   getT.appendChild(sortedLinks[l].el);
>
> > }
>
> > I've tried:
>
> > function sortText(a, b) {
>
> >   if ( a.text < b.text ) return -1;
> >   else if ( a.text > b.text ) return 1;
> >   return 0;}
>
> > var meh=[];
>
> > $('#test a').each(function(){
>
> >     meh[i] = { text: $(this).text(), el: $(this) };
>
> > });
>
> > var sortedLinks = meh.sort( sortText );
>
> > $(sortedLinks).each(function(l){
>
> >         $('#test).append([l].el);
>
> > });
>
> > ...but that doesn't seem to work.
>
> > I've also tried using $.map, but I couldn't figure it out.

Reply via email to