This should do it:
array.sort(function(a, b) {
return (a<2) - (b<2) || a - b;
});
The first condition returns 1
or -1
depending on whether a
or b
are smaller than 2
, or 0
if both are on the same "side" of 2
, in which case the second (standard number) comparison takes place.