I have this JavaScript array of Objects:
data = [
{'deviation': 57.41, 'provider': "This", 'measure': "median"},
{'deviation': 49.02, 'provider': "All", 'measure': "median"},
{'deviation': 199.67, 'provider': "This", 'measure': "third_quartile"},
{'deviation': 152.31, 'provider': "All", 'measure': "third_quartile"},
{'deviation': 41.48, 'provider': "This", 'measure': "first_quartile"},
{'deviation': -1.07, 'provider': "All", 'measure': "first_quartile"}
]
and I would like to sort it both by 'provider' (the three "This" before the three "All") and by 'measure' (first quartile, median, third quartile), so the resulting array will look like:
data = [
{'deviation': 41.48, 'provider': "This", 'measure': "first_quartile"},
{'deviation': 57.41, 'provider': "This", 'measure': "median"},
{'deviation': 199.67, 'provider': "This", 'measure': "third_quartile"},
{'deviation': -1.07, 'provider': "All", 'measure': "first_quartile"}
{'deviation': 49.02, 'provider': "All", 'measure': "median"},
{'deviation': 152.31, 'provider': "All", 'measure': "third_quartile"},
]
I have written a function as an argument to .sort()
, and it does return the array sorted by 'provider', but then when I feed it into the same function with measure
as an argument (thankfully, first_quartile, median, third_quartile are already alphabetically sorted the way I want them) - the sorting gets broken. How can I go about doing it?
EDIT
The functions I have been using:
var compare_prv = function(a,b) {
if (a.provider < b.provider){
return 1;
}
return 0;
}
var compare_meas = function(a,b) {
if (a.measure < b.measure){
return -1;
}
return 0;
}
See Question&Answers more detail:
os