jQuery-对数组进行排序? [英] jQuery - Sorting an array?
问题描述
我正在使用Ajax来获取一些XML,然后使用结果填充表单上的某些字段.表单上有一个数字字段,我想按此数字对结果进行排序(从高到低).
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).
我将如何在jQuery中做到这一点?
How would I go about doing this in jQuery?
我的js函数代码当前为:
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");}
});
}
Links
标签是我想要对其进行排序的标签.
The Links
tag is the one I'm wanting to sort it on.
谢谢
作为参考,返回的XML如下:
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>
推荐答案
首先,我制作了一个数组,该数组的元素由保存网址和链接的Objects组成.之后,我对其进行了排序,并用数据填充了字段.
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.
代码如下:
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.
这篇关于jQuery-对数组进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!