开发者

jQuery - Sorting an array?

开发者 https://www.devze.com 2022-12-29 15:17 出处:网络
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).

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.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 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.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>


Firstly, I made an array with elements consisting of Objects that hold the url and the links. After then, I sorted it and filled fields with the 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 haven't tested with stub data, so it might have some errors, but you may can fix it.

0

精彩评论

暂无评论...
验证码 换一张
取 消