简体   繁体   English

如何根据特定值对json数据进行排序?

[英]How to sort json data according to specific value?

I am using this code to get the following Json data 我正在使用此代码来获取以下Json数据

function nextDepartures(minutes=120)
{
var modeId = 0;
var stopId = 1042;
var limit = 2;

xhr(broadNextDeparturesURL(modeId, stopId, limit), broadNextDeparturesCallback);

var nowDate = new Date();
nowDate.setMinutes(nowDate.getMinutes()+minutes); 



for(var i =0; i < broadjson[0].values.length;i++) {
    var date = new Date(broadjson[0].values[i].time_timetable_utc);
    var minutes;
    if (date < nowDate) {
        if (date.getMinutes() < 10) {
            minutes = "0" + date.getMinutes();
        }
        else {
            minutes = date.getMinutes();
        }

    else  {

        document.getElementById("depart").innerHTML += "<tr>" +
            "<td width='30%'>" + date.getHours() + ":" + minutes + "</td>" +
            "<td>" + broadjson[0].values[i].platform.direction.line.line_number +
            " "
            + broadjson[0].values[i].platform.direction.direction_name + "</td>" +

            "</tr>";
    }
    }
}
}

and in return I am getting this data 作为回报,我得到了这些数据

{
"values": [
{
"platform": {
"realtime_id": 0,
"stop": {
"distance": 0.0,
"suburb": "East Melbourne",
"transport_type": "train",
"route_type": 0,
"stop_id": 1104,
"location_name": "Jolimont-MCG",
"lat": -37.81653,
"lon": 144.9841
},
"direction": {
"linedir_id": 38,
"direction_id": 5,
"direction_name": "South Morang",
"line": {
"transport_type": "train",
"route_type": 0,
"line_id": 5,
"line_name": "South Morang",
"line_number": "250",
"line_name_short": "South Morang",
"line_number_long": ""
}
}
},
"run": {
"transport_type": "train",
"route_type": 0,
"run_id": 15716,
"num_skipped": 0,
"destination_id": 1041,
"destination_name": "Clifton Hill"
},
"time_timetable_utc": "2016-03-16T01:51:00Z",
"time_realtime_utc": null,
"flags": "",
"disruptions": ""
}
]
}

I want to sort this data according to 我想根据这些数据进行排序

values.platform.direction.line.line_number

which means the lowest line number will be displayed first and the highest line number at last. 这意味着最低的行号将首先显示,最后显示最高的行号。 I tried Javascript function sort(a,b) but its not working. 我尝试了Javascript函数sort(a,b),但是它不起作用。 The problem is that 问题是

values.platform.direction.line.line_number

is a string value. 是一个字符串值。 so how can I sort the array according to line_number which returns integer but in form of a string? 所以我怎样才能根据line_number排序数组,它返回整数但以字符串形式?

Use parseInt to parse the number passed in as string and sort the array using javascript sort function as below. 使用parseInt解析作为字符串传入的数字,并使用javascript sort函数对数组进行排序,如下所示。

myArray.sort(function(a, b){
    var lineA = parseInt(a.line_number),
        lineB = parseInt(b.line_number);
    if(lineA < lineB)  return -1;
    if(lineA > lineB) return 1;
    return 0;
});

You could sort directly with the delta of line_number . 您可以直接使用line_number的增量进行排序。 The minus cast both parts to number. 减号将两个部分都转换为数字。

 var data = { values: [ { platform: { direction: { line: { line_number: "250" } } } }, { platform: { direction: { line: { line_number: "150" } } } }, { platform: { direction: { line: { line_number: "110" } } } } ] }; data.values.sort(function (a, b) { return a.platform.direction.line.line_number - b.platform.direction.line.line_number; }); console.log(data); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM