Javascript 按静态值范围对数组排序

Javascript 按静态值范围对数组排序,javascript,arrays,sorting,Javascript,Arrays,Sorting,我有一个数组,为了简单起见,可以这样说: var array = [2,55, 22, 6, 7]; 我想对它进行分类,我会这样做: array.sort(function (a, b) { return b > a ? -1: 1; }); array.sort(function (a, b) { return b < a ? -1: 1; }); 或者,如果我想按升序排序,我会这样做: array.sort(function (a, b) { ret

我有一个数组,为了简单起见,可以这样说:

var array = [2,55, 22, 6, 7];
我想对它进行分类,我会这样做:

array.sort(function (a, b) {
    return b > a ? -1: 1;
});
array.sort(function (a, b) {
    return b < a ? -1: 1;
});
或者,如果我想按升序排序,我会这样做:

array.sort(function (a, b) {
    return b > a ? -1: 1;
});
array.sort(function (a, b) {
    return b < a ? -1: 1;
});
array.sort(函数(a,b){
返回b
现在,假设我有一个我想要排序的值。 所以基本上我想按大于一个数的值对数组进行排序。 如果数字是6,我希望我的数组看起来像这样:

// 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;
        }
    }
});
55,22,7,6,2

但如果我想在两个数字之间排序,比如说6和23,我希望它返回如下内容:

// 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;
        }
    }
});
22,7,55,6,2

老实说,最后3项可以以任何顺序出现,但我排序的范围必须首先出现

有人知道我怎样才能做到这一点吗。 我试过这样做:

// 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;
        }
    }
});
//对映射数组进行排序
映射.排序(函数(a,b){
//循环遍历我们的属性
对于(变量i=0;i'){
//检查该值是否与表达式匹配
返回y如果(field.operator==”您的比较函数需要检查某个项目是否在范围内,然后将其放在范围外的项目之前。如果两者都在范围内,或者两者都在范围外,则必须像往常一样进行比较

以下代码执行以下操作:

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);
});
var数组=[2,55,22,6,7];
var低=6,
高=23;
array.sort(函数(a,b){
返回((b>=低和b<高)-(a>=低和a<高)| |(a-b);
});

此函数对数组进行排序,然后将大于给定最小值的所有数字推送到数组的后面

编辑:只调用一次
splice()
可以提高性能

var数组=[2,55,22,6,7];
功能排序(arr,最小值){
var sorted=arr.sort(函数(a,b){
返回a-b;
});
var-index=sorted.length-1;
while(排序[索引]>min){
指数--;
}
var tmp=sorted.splice(拼接索引,sorted.length-1-拼接索引);
tmp.forEach(功能(el,i){
已排序。取消移位(el);
});
返回排序;
}

log(sortBetween(array,6));
如果数组元素不在您关心的范围内,只需将其替换为一个大得离谱的值,这样它就会移动到列表的末尾

由于
a
b
具有局部作用域,因此这不会影响实际数组,只会影响比较

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;
});
array.sort(函数(a,b){
如果(a23)a=Number.MAX\u SAFE\u整数;
如果(b23)b=Number.MAX\u SAFE\u整数;
返回b>a-1:1;
});

你应该看看你到底需要所有的值吗?
数组。过滤器(x=>6>=x&&xa-b)
可能是最简单的解决方案。这是可行的,但是重复的
拼接
效率非常低。为什么
数字。最大安全整数
?如果你想要一个“大得离谱的值”,只要选择
无限
:-)顺便说一句,如果他们相等,你会先测试一下,然后再接受:)