Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/409.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript排序函数未按预期工作_Javascript_Sorting - Fatal编程技术网

JavaScript排序函数未按预期工作

JavaScript排序函数未按预期工作,javascript,sorting,Javascript,Sorting,我正在尝试对数组进行排序: var mapped = [{ wiFi: true, megapixel: '20 MP' },{ wiFi: false, megapixel: '25 MP' },{ wiFi: true, megapixel: '25 MP' },{ wiFi: true, megapixel: '21 MP' }]; 我想对这两个属性进行排序(wiFi优先)。这是我正在做的一个非常简单的版本,但是属性可以由用

我正在尝试对数组进行排序:

var mapped = [{
    wiFi: true,
    megapixel: '20 MP'
},{
    wiFi: false,
    megapixel: '25 MP'
},{
    wiFi: true,
    megapixel: '25 MP'
},{
    wiFi: true,
    megapixel: '21 MP'
}];
我想对这两个属性进行排序(wiFi优先)。这是我正在做的一个非常简单的版本,但是属性可以由用户决定,所以我有一个函数看起来有点像这样:

var fields = ['wiFi', 'megapixel'];

// Sort our mapped array
mapped.sort(function (a, b) {

    // Loop through our properties
    fields.forEach(function (field) {

        // Get our value (skip the first)
        var x = a[field.name];
        var y = b[field.name];

        // Switch on the type
        switch (typeof x) {

            // Boolean
            case "boolean":

                // Return our value
                return x === y ? 0 : x ? -1 : 1;

            // Default
            default:

                // Return our value
                return x === y ? 0 : x < y ? -1 : 1;

        }
    });
});
var字段=['wiFi','megapixel'];
//对映射数组进行排序
映射.排序(函数(a,b){
//循环遍历我们的属性
fields.forEach(函数(字段){
//获取我们的价值(跳过第一个)
var x=a[field.name];
变量y=b[field.name];
//打开类型
开关(x型){
//布尔值
案例“布尔”:
//回报我们的价值
返回x==y?0:x?-1:1;
//违约
违约:
//回报我们的价值
返回x==y?0:x

但是我的数组根本没有被排序。有人知道为什么吗?

尝试手动迭代,而不是使用内部函数

mapped.sort(function(a,b) {
    var x, y, i, l = fields.length;
    for( i=0; i<l; i++) {
        x = a[fields[i]];
        y = b[fields[i]];
        if( x === y) continue; // proceed to next field to compare
        switch(typeof x) {
        case "boolean":
            return x ? -1 : 1;
        // could do other things, eg. case "string": return x.localeCompare(y);
        default:
            return x < y ? -1 : 1;
        }
    }
    return 0; // all fields compared equal
});
mapped.sort(函数(a,b){
变量x,y,i,l=字段长度;

对于(i=0;i多亏了你的建议,我尝试将它们结合起来,得出以下结论:

// Sort our mapped array
mapped.sort(function (a, b) {

    // Loop through our properties
    for (var i = 0; i < fields.length; i++) {

        // Get our field
        var field = fields[i];

        // Get our value (skip the first)
        var x = a[field.name];
        var y = b[field.name];

        // If our values are the same, go to the next compare
        if (x === y)
            continue;

        // Switch on the type
        switch (field.type) {

            // Boolean
            case "boolean":

                // Return our value
                return x ? -1 : 1;

            // Integer
            case "float":

                // Update our values
                x = parseFloat(x);
                y = parseFloat(y);

                // Return our value
                return x < y ? -1 : 1;

            // Default (string)
            default:

                // Return our value
                return x.localeCompare(y);
        }
    }
//对映射数组进行排序
映射.排序(函数(a,b){
//循环遍历我们的属性
对于(变量i=0;i
您可以对助手对象使用排序

var-mapped=[{wiFi:true,百万像素:“20MP”},{wiFi:false,百万像素:“25MP”},{wiFi:true,百万像素:“25MP”},{wiFi:true,百万像素:“21MP”},
字段=['wiFi','megapixel'],
类型={
wiFi:function(o){return+!o.wiFi;},//如果为true,则排序顶部
百万像素:函数(o){return parseFloat(o.megapixel)| | 0;},
};
映射.排序(函数(a,b){
var r=0;
一些(函数(c){
返回(r=类型[c](a)-类型[c](b));
});
返回r;
});

document.write(''+JSON.stringify(mapped,0,4)+'');
您可以尝试计算排名,并根据排名对值进行排序:

逻辑:
  • 对于布尔值,如果为true,则在self中添加10,并从其他中删除10
  • 对于其他人,您可以使用通用逻辑,在较大的值中添加10,并从较小的值中减少10
  • 根据这个等级排序
var-mapped=[{wiFi:true,百万像素:'20MP'},{wiFi:false,百万像素:'25MP'},{wiFi:true,百万像素:'25MP'},{wiFi:true,百万像素:'21MP'}];
var字段=['wiFi','megapixel'];
//对映射数组进行排序
映射.排序(函数(a,b){
var a_秩=0;
var b_秩=0;
//循环遍历我们的属性
fields.forEach(函数(字段){
//获取我们的价值(跳过第一个)
var x=一个[字段];
变量y=b[字段];
//打开类型
开关(x型){
//布尔值
案例“布尔”:
a_秩+=x?-10:10;
b_秩+=y?-10:10;
打破
//违约
违约:
如果(x>y){
a_秩+=10;
b_秩-=10;
}如果(y>x),则为else{
a_秩-=10;
b_秩+=10
}
打破
}
});
返回a_秩>b_秩?1:a_秩document.write(“+JSON.stringify(mapped,0,4)+”)
Yes,因为您实际上并没有从排序回调返回任何结果-只从内部
forEach
回调返回。您不需要迭代
字段
,直到结束。当您不返回0时停止。您可以发布一个示例吗?