I have the follow json object
[
{"PARTNERNAME":"Partner 1","DISTANCE":20,"TYPE":"1"},
{"PARTNERNAME":"Partner 2","DISTANCE":14,"TYPE":"2"},
{"PARTNERNAME":"Partner 3","DISTANCE":60,"TYPE":"2"},
{"PARTNERNAME":"Partner 4","DISTANCE":37,"TYPE":"1"},
{"PARTNERNAME":"Partner 5","DISTANCE":25,"TYPE":"2"},
{"PARTNERNAME":"Partner 6","DISTANCE":90,"TYPE":"1"},
{"PARTNERNAME":"Partner 7","DISTANCE":49,"TYPE":"1"}
]
I'd like to sort it first by type and then by distance so the result would be the following.
[
{"PARTNERNAME":"Partner 1","DISTANCE":20,"TYPE":"1"},
{"PARTNERNAME":"Partner 4","DISTANCE":37,"TYPE":"1"},
{"PARTNERNAME":"Partner 7","DISTANCE":49,"TYPE":"1"},
{"PARTNERNAME":"Partner 6","DISTANCE":90,"TYPE":"1"},
{"PARTNERNAME":"Partner 2","DISTANCE":14,"TYPE":"2"},
{"PARTNERNAME":"Partner 5","DISTANCE":25,"TYPE":"2"},
{"PARTNERNAME":"Partner 3","DISTANCE":60,"TYPE":"2"}
]
I have the following code that sorts it by one field but I can get it to sort by two fields. Is this possible?
var sortedData = propertyArray.sort(sortByProperty('DISTANCE'));
function sortByProperty(property) {
'use strict';
return function (a, b) {
var sortStatus = 0;
if (a[property] < b[property]) {
sortStatus = -1;
} else if (a[property] > b[property]) {
sortStatus = 1;
}
return sortStatus;
};
}