简体   繁体   English

将无限数量的参数传递给sort()函数

[英]Pass unlimited number of arguments to sort() function

The task at hand is to create a function that takes an unlimited number of arguments to sort the array by. 当前的任务是创建一个函数,该函数使用不限数量的参数来对数组进行排序。

For instance: 例如:

var data = [['John','London',35],
            ['Ricky','Kuala Lumpur',38],
            ['Ivan','Moscow,29]];

function arrangeOrder(a,b) {
 //Sort by field [0]
 if (a[0] > b[0]) return 1;
 if (a[0] < b[0]) return -1;

 //Sort by field [1]
 if (a[1] > b[1]) return 1;
 if (a[1] < b[1]) return -1;

 //Sort by field [2]
 if (a[2] > b[2]) return 1;
 if (a[2] < b[2]) return -1;
}

data.sort(arrangeOrder);

Works perfectly! 完美的作品! However, the data content and the number of fields can change, as well as the fields and the number of fields that the data need to be sorted by. 但是, data内容和字段数以及需要对数据进行排序的字段和字段数都可以更改。 So what I need is to be able to specify the function is this way: 所以我需要能够以这种方式指定功能:

data.sort(arrangeOrder(1,3,6,9,2));

I found the way to add ONE parameter: 我找到了添加一个参数的方法:

function propComparator(prop) {
  return function(a,b){
    return a[prop] - b[prop];
  }
}
data.sort(propComparator(prop));

As well as the way to process undifened number of arguments for the function using well arguments object: 以及使用well arguments对象处理函数的参数数量不确定的方法:

function foo() {
  for (var i = 0; i < arguments.length; i++) {
    console.log(arguments[i]);
  }
}

But I can't seem to make this work in combination. 但我似乎无法将这项工作组合在一起。 It should be possible, but I guess I just lack the skill. 应该有可能,但是我想我只是缺乏技巧。

NOTE: ES6 solutions do not work in my environment, unfortunately, so I need vanilla JavaScript! 注意:不幸的是,ES6解决方案在我的环境中不起作用,因此我需要普通的JavaScript! Appreciate all the help I can get! 感谢所有我能得到的帮助!

Use a simple for loop: 使用简单的for循环:

function arrangeOrder(a, b) {
  for (var i = 0; i < a.length; i++) {
    if (a[i] > b[i]) return 1;
    if (a[i] < b[i]) return -1;
  }
}

If you want to specify the order the indexes passed in as arguments, you can make a function that reads those arguments and returns a new function that you can pass to sort() . 如果要指定索引作为参数传入的顺序,则可以使函数读取这些参数并返回一个新函数,该函数可以传递给sort()

 var data = [['Betty','London',35], ['Xavier','Kuala Lumpur',38], ['Alan','Moscow',35]]; function sortFunc(){ let args = arguments return function(a, b) { for (let i = 0; i < args.length; i++){ if (a[args[i]] === b[args[i]]){ continue } return a[args[i]] > b[args[i]] ? 1 : -1 } return 0 } } // Sort by age, then name, then city let f = sortFunc(2, 0, 1) data.sort(f) console.log(data) // Sort by age, then city, then name f = sortFunc(2, 1, 0) data.sort(f) console.log(data) // Also works with objects: var data = [ {name: "Mark", age:25, home: "Anchorage"}, {name: "Mark", age:15, home: "New York"}, {name: "John", age:25, home: "Aachen"}, {name: "Alice", age:15, home: "Boston"} ]; f = sortFunc("age", "home", "name") data.sort(f) console.log(data) 

you have already answred yourself [ I found the way to add ONE parameter ] 您已经生气了 [ 我找到了添加一个参数的方法 ]

var data =  [['John','London',35],
            ['Ricky','Kuala Lumpur',38],
            ['Ivan','Moscow',29]];

function propComparator(prop) {
    return function(a,b){
        for (const x of prop) {
            if ( a.hasOwnProperty(prop[x]) && b.hasOwnProperty(prop[x]) ) {
                if (a[ prop[x] ] === b[ prop[x] ]) continue;
                return a[ prop[x] ] > b[ prop[x] ]?1:-1;
            } else
                continue;
        }
    }
}
var prop = [2, 0, 1, 5, 1000];
data.sort(propComparator(prop));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM