在客户端对JSON数据进行排序

在客户端对JSON数据进行排序,json,jquery,Json,Jquery,我有以下脚本,它将2个提要组合在一起: $(document).Ready(function() { url = 'feed 1'; url_2 = 'feed 2'; $.when( $.ajax({ type: "GET", url: document.location.protocol + '//ajax.googleapis.com/ajax/services/feed/load?v=1.0&

我有以下脚本,它将2个提要组合在一起:

$(document).Ready(function() {
    url = 'feed 1';
    url_2 = 'feed 2';

    $.when(
        $.ajax({
            type: "GET",
            url: document.location.protocol + '//ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=1000&callback=?&q=' + encodeURIComponent(url),
            dataType: 'json'
        }),
        $.ajax({
            type: "GET",
            url: document.location.protocol + '//ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=1000&callback=?&q=' + encodeURIComponent(url_2),
            dataType: 'json'
        })
    ).done(function(a1, a2) {
        var data = a1[0].responseData.feed.entries.concat(a2[0].responseData.feed.entries);
        if (data[0]) {
            for (i = 0; i <= data.length - 1; i++) {
                document.write(data[i].title);
                document.write("<br>");
                document.write(data[i].categories[0]);
                document.write("<br>");
                document.write(data[i].publishedDate);
                document.write("<br>");
                document.write(data[i].link);
                document.write("<hr>");
            }
        } 
        else {
            document.write("No records");
        }
    });
});
$(文档).Ready(函数(){
url='feed 1';
url_2=‘提要2’;
美元。什么时候(
$.ajax({
键入:“获取”,
url:document.location.protocol+'//ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=1000&callback=?&q='+encodeURIComponent(url),
数据类型:“json”
}),
$.ajax({
键入:“获取”,
url:document.location.protocol+'//ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=1000&callback=?&q='+encodeURIComponent(url_2),
数据类型:“json”
})
).完成(功能(a1、a2){
var data=a1[0].responseData.feed.entries.concat(a2[0].responseData.feed.entries);
如果(数据[0]){

对于(i=0;i你可以这样做

data.sort(function (a, b) {
    if (a.v > b.v) {
        return 1
    }
    if (a.v < b.v) {
        return -1
    }
    return 0;
});
data.sort(函数(a,b){
如果(a.v>b.v){
返回1
}
如果(a.v
注释

  • v
    更改为已发布数据的变量名
  • 数据
    应该是一个提要数组

这似乎给了我奇怪的结果,即2013年8月12日、2013年9月16日、2013年5月12日。我原本预计,2013年9月16日、2013年8月12日、2013年5月12日。@oshirowanen将
return 1
更改为
return-1
,反之亦然,似乎仍然是完全随机的,即在同一个月和同一年,我得到2013年9月19日、22日、15日,然后是12日2013年5月?@oshirowanen你能创建一个JSFIDLE吗,然后我会在那里给它一个重击