jQuery can parse most text to xml but that doesn't work in all cases:
http://groups.google.com/group/jquery-en/browse_frm/thread/95718c9aab2c7483/af37adcb54b816c3?lnk=gst&q=parsexml#af37adcb54b816c3
You can also copy Array.prototype.sort to $.fn and it will work on the
jQuery object as well:

function parseXML( xml ) {
 if( window.ActiveXObject && window.GetObject ) {
  var dom = new ActiveXObject( 'Microsoft.XMLDOM' );
  dom.loadXML( xml );
  return dom;
 }
  if( window.DOMParser )
   return new DOMParser().parseFromString( xml, 'text/xml' );
   throw new Error( 'No XML parser available' );
}

var xml = '<base>';
xml += '<item type="c">one</item>';
xml += '<item type="b">two</item>';
xml += '<item type="d">three</item>';
xml += '<item type="f">three</item>';
xml += '<item type="a">three</item>';
xml += '</base>';

$.fn.sort=[].sort; //copy the array method

$(parseXML(xml).childNodes[0].childNodes)
.sort(function(a,b){  //using the native sort here
   var at = $(a).attr('type'), bt = $(b).attr('type');
   return (at < bt) ? -1 : 1;
});

On Jan 22, 4:36 pm, skube <sku...@gmail.com> wrote:
> Thanks for the reply, but that doesn't seem to be an XML object at
> all. Isn't that simply a string that looks like XML. Also, by using get
> () aren't you simply converting that string into an array anyway?
>
> On Jan 22, 3:00 am, Ricardo Tomasi <ricardob...@gmail.com> wrote:
>
> > What kind ofsorting?
>
> > xml= '<base>';xml+= '<item type="c">one</item>';xml+= '<item 
> > type="b">two</item>';xml+= '<item type="d">three</item>';xml+= '<item 
> > type="f">three</item>';xml+= '<item type="a">three</item>';xml+= '</base>';
>
> > var sorted = $(xml).filter('item').get().sort(function(a,b){
> >    var at = $(a).attr('type'), bt = $(b).attr('type');
> >    return (at < bt) ? -1 : 1;
>
> > });
>
> > $(sorted).each(function(){
> >   console.log( this );
>
> > });
>
> >https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Global...
>
> > - ricardo
>
> > On Jan 21, 10:01 pm, skube <sku...@gmail.com> wrote:
>
> > > It seems jQuery is really great at traversing anXMLobject. It is
> > > quite easy to gather and filter node data. However I can't seem to
> > > find any information regardingsortingXMLusing jQuery. Is it
> > > possible? Or is there a plug-in?
>
> > > I have a project which requires me to read anXMLfile, then filter
> > > and sort the data. Filtering I can do, but I have no idea how to sort
> > >XML. Since arrays are easily sortable in JS, my idea is to convert the
> > >XMLinto an array first, then filter the array (which is not as easy
> > > as filteringXMLw/ jQuery) then sort the array.
>
> > > Does anyone have any better ideas?
>
> > > thanks,
> > > skube

Reply via email to