jQuery - Sorting an array?

Posted by Probocop on Stack Overflow See other posts from Stack Overflow or by Probocop
Published on 2010-05-06T10:02:38Z Indexed on 2010/05/06 10:08 UTC
Read the original article Hit count: 225

Filed under:

Hi, I'm using Ajax to get some XML, and then filling in some fields on a form with the results. There is a numerical field on the form and I would like to sort the results by this number (highest first).

How would I go about doing this in jQuery?

My js function code is currently:

function linkCounts() {
    ws_url = "http://archreport.epiphanydev2.co.uk/worker.php?query=linkcounts&domain="+$('#hidden_the_domain').val();
    $.ajax({
        type: "GET",
        url: ws_url,
        dataType: "xml",
        success: function(xmlIn){
            results = xmlIn.getElementsByTagName("URL");
            for ( var i = 0; i < results.length; i++ ) {
                $("#tb_domain_linkcount_url_"+(i+1)).val($(results[i].getElementsByTagName("Page")).text());
                $("#tb_domain_linkcount_num_"+(i+1)).val($(results[i].getElementsByTagName("Links")).text());
            }
            $('#img_linkcount_worked').attr("src","/images/worked.jpg");
        },
        error: function(){$('#img_linkcount_worked').attr("src","/images/failed.jpg");}
    });
}

The Links tag is the one I'm wanting to sort it on.

Thanks

For reference the XML that's getting returned is like the following:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Response>
  <ResponseCode>1</ResponseCode>
  <ResponseStatus>OK</ResponseStatus>
  <ReportId>2</ReportId>
  <UrlChecked />
  <MaxLinks>75</MaxLinks>
  <PagesFound>121</PagesFound>

  <URLs>
<URL>
      <Page>http://www.epiphanysolutions.co.uk/blog</Page>
      <Links>78</Links>
    </URL>
    <URL>
      <Page>http://www.epiphanysolutions.co.uk/blog/</Page>

      <Links>78</Links>
    </URL>
    <URL>
      <Page>http://www.epiphanysolutions.co.uk/blog/author/daniel-peden/</Page>
      <Links>78</Links>
    </URL>
    <URL>

      <Page>http://www.epiphanysolutions.co.uk/blog/author/daniel-peden/page/2/</Page>
      <Links>78</Links>
    </URL>
</URLS>
</Response>

© Stack Overflow or respective owner

Related posts about jQuery