JQuery - Sorting an array?

I am using Ajax to get some XML and then fill in some form fields with the results. There is a numeric field in the form, and I would like to sort the results by that number (height first).

How can I do this in jQuery?

My js function code is currently:

function linkCounts() {
    ws_url = "http://archreport.example.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 tag Links

is the one I want to sort.

thanks

For reference, the XML returned looks like this:

<?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.example.co.uk/blog</Page>
      <Links>78</Links>
    </URL>
    <URL>
      <Page>http://www.example.co.uk/blog/</Page>

      <Links>78</Links>
    </URL>
    <URL>
      <Page>http://www.example.co.uk/blog/author/example/</Page>
      <Links>78</Links>
    </URL>
    <URL>

      <Page>http://www.example.co.uk/blog/author/example/page/2/</Page>
      <Links>78</Links>
    </URL>
</URLS>
</Response>

      

+2


a source to share


1 answer


First, I created an array with items made up of objects that contain a url and links. After that, I sorted it and filled the fields with data.

The code looks like this:



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");

            var container = [];
            for ( var i = 0; i < results.length; i++ ) {
                container[i] = {
                    url: $(results[i].getElementsByTagName("Page")).text(),
                    links: $(results[i].getElementsByTagName("Links")).text()
                }                
            }

            container.sort(function(a, b) {
                return b.links - a.links;
            });

            for ( var i = 0; i < results.length; i++ ) {
                $("#tb_domain_linkcount_url_"+(i+1)).val(container.url);
                $("#tb_domain_linkcount_num_"+(i+1)).val(container.links);
            }

            $('#img_linkcount_worked').attr("src","/images/worked.jpg");
        },
        error: function(){$('#img_linkcount_worked').attr("src","/images/failed.jpg");}
    });
}

      

I have not tested the stub data, so it may have some bugs, but you can fix them.

+4


a source







All Articles