1

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.

r3plica
  • 13,017
  • 23
  • 128
  • 290
  • You should have a look at [Sorting in JavaScript: Should every compare function have a “return 0” statement?](http://stackoverflow.com/q/20883421/1048572) – Bergi Oct 18 '16 at 23:56
  • Do you need all the values at all? `array.filter(x => 6>=x && x<23).sort((a,b) => a-b)` could be the most simple solution. – Bergi Oct 19 '16 at 00:05

3 Answers3

1

Your comparison function will need to check whether an item is in the range and then put that before an item outside of the range. If both are inside, or both are outside, they have to be compared as usual.

The following code does that:

var array = [2,55, 22, 6, 7];
var low = 6,
    high = 23;
array.sort(function(a, b) {
    return ((b >= low && b < high) - (a >= low && a < high)) || (a - b);
});
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
0

This function sorts your array and then pushes all numbers bigger than the given minimum value to the back of the array.

EDIT: Better performance by calling splice() only once.

var array = [2,55, 22, 6, 7];

function sortBetween(arr, min){
  var sorted = arr.sort(function(a, b) {
    return a - b;
  });
  var spliceIndex = sorted.length - 1;
  while(sorted[spliceIndex] > min) {
    spliceIndex--;
  }
  
  var tmp = sorted.splice(spliceIndex, sorted.length - 1 - spliceIndex);
  tmp.forEach(function(el, i) {
    sorted.unshift(el);
  });
    
  return sorted;
}
console.log(sortBetween(array, 6));

Result is, that your sorted array starts with your minimum number. There is no need to set the maximum, because you said:

the last 3 items can appear in any order to be honest, but the range I have sorted must appear first.

Dario
  • 582
  • 1
  • 5
  • 17
0

If an array element falls out of the range you care about, just replace it with an absurdly large value so it moves to the end of the list.

Because a and b have local scope, this won't affect the actual array, just the comparison.

array.sort(function (a, b) {
    if (a<6 || a>23) a = Number.MAX_SAFE_INTEGER;
    if (b<6 || b>23) b = Number.MAX_SAFE_INTEGER;
    return b > a ? -1: 1;
});
John Wu
  • 50,556
  • 8
  • 44
  • 80
  • Why `Number.MAX_SAFE_INTEGER`? If you want an "absurdely large value", just go for `Infinity` :-) – Bergi Oct 19 '16 at 00:35
  • And btw, you [should not forget to `return 0`](http://stackoverflow.com/q/20883421/1048572) in case they are equal – Bergi Oct 19 '16 at 00:36