Garry
Garry

Reputation: 342

How to sort json data according to specific value?

I am using this code to get the following Json data

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

Upvotes: 2

Views: 4421

Answers (2)

Nina Scholz
Nina Scholz

Reputation: 386654

You could sort directly with the delta of 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; }

Upvotes: 1

z0mBi3
z0mBi3

Reputation: 1544

Use parseInt to parse the number passed in as string and sort the array using javascript sort function as below.

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;
});

Upvotes: 2

Related Questions