I have an array, for simplicity, lets say it is this:
var array = [2,55, 22, 6, 7];
and I want to sort it, I would do this:
array.sort(function (a, b) {
return b > a ? -1: 1;
});
or if I want to sort it ascending I would do this:
array.sort(function (a, b) {
return b < a ? -1: 1;
});
Now, let's say I have a value that I want to sort by. So basically I want to sort my array by values greater than a number. so if the number was 6, I would want my array to look something like this:
55, 22, 7, 6, 2
but if I want to sort between two numbers, let's say 6 and 23, I would want it to return something like this:
22, 7, 55, 6, 2
the last 3 items can appear in any order to be honest, but the range I have sorted must appear first.
Does anyone know how I can achieve this. I have tried like this:
// Sort our mapped array
mapped.sort(function (a, b) {
// Loop through our properties
for (var i = 0; i < fields.length; i++) {
// Get our value (skip the first)
var field = fields[i],
x = a[field.name],
y = b[field.name];
// If our values are the same, go to the next compare
if (x === y)
continue;
// If we are using greater than
if (field.operator === '>') {
// Check that the value matches our expression
return y < field.expression ? -1 : 1;
}
// If we are using less than
if (field.operator === '<') {
// Check that the value matches our expression
return y > field.expression ? -1 : 1;
}
}
});
field.expression holds the range value. As you can see I am doing a loop around my fields and then trying to sort.