I am trying to sort some numbers in Javascript - this works fine, but I am trying to refactor some of the code so I am not hard coding field names. Below is my function:
data.sort(function(a, b) {
return a.values[0].field1 - b.values[0].field1;
});
The problem is that I want to also use this same functionality with field2
I've tried doing something like the following (simplified):
var sortBy = "field1";
data.sort(function(a, b) {
return a.values[0].sortBy- b.values[0].sortBy;
});
That is looking for a literal field name of sortBy though and is not working. Any help would be appreciated.