2

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?

Garry
  • 342
  • 5
  • 18
  • 1
    Use javascript sort function with compare param – Iññoceñt Ùšmâñ Aug 31 '16 at 05:18
  • 1
    Check this answer http://stackoverflow.com/questions/881510/sorting-json-by-values – frankfg Aug 31 '16 at 05:21
  • 1
    You can parseInt and compare the value using javascript sort function. – z0mBi3 Aug 31 '16 at 05:22
  • 1
    Possible duplicate of [Sort array of objects by single key with date value](http://stackoverflow.com/questions/8837454/sort-array-of-objects-by-single-key-with-date-value) – nikjohn Aug 31 '16 at 05:26
  • A similar question was asked before in stackoverflow. Check out this link. https://stackoverflow.com/questions/24736852/how-to-sort-json-by-a-single-integer-field – Phoenix Aug 31 '16 at 05:19

2 Answers2

2

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;
});
z0mBi3
  • 1,534
  • 8
  • 9
1

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; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392