jQuery - 对数组进行排序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2780174/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery - Sorting an array?
提问by Probocop
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).
我正在使用 Ajax 来获取一些 XML,然后用结果填写表单上的一些字段。表单上有一个数字字段,我想按这个数字(最高的在前)对结果进行排序。
How would I go about doing this in jQuery?
我将如何在 jQuery 中执行此操作?
My js function code is currently:
我的js函数代码目前是:
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.
Links
标签是我想要对其进行排序的标签。
Thanks
谢谢
For reference the XML that's getting returned is like the following:
作为参考,返回的 XML 如下所示:
<?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>
回答by Sanghyun Lee
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.
首先,我创建了一个数组,其中的元素由包含 url 和链接的对象组成。之后,我对它进行排序并用数据填充字段。
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.
我没有用存根数据进行测试,所以它可能有一些错误,但你可以修复它。