JavaScript 排序比较器函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17382091/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 08:06:34  来源:igfitidea点击:

JavaScript sort comparator function

javascript

提问by Stanimirovv

Basically I want to build a function which sorts objects in an array by one of the object's properties/member variables. I am preeeety sure that the comparator function is where the error is hidden, but I am not 100% sure.

基本上我想构建一个函数,通过对象的属性/成员变量之一对数组中的对象进行排序。我很确定比较器函数是隐藏错误的地方,但我不是 100% 确定。

The output I should get after the sort function is called is 1,2,3. I get 1,3,2which means that it is unchanged

调用排序函数后我应该得到的输出是1,2,3. 我明白1,3,2这意味着它没有改变

This is the entire js code (with some comments):

这是整个js代码(有一些注释):

var arr = [];
//object definition and creation
var main = document.getElementById("main");
var task = {
    name: "",
    priority: 0
};

//first
var one = Object.create(task);
one.priority = 1;
//secondd
var two = Object.create(task)
two.priority = 3;
//last
var three = Object.create(task);
three.priority = 2;

//append
arr.push(one);
arr.push(two);
arr.push(three);

//sort function
function sortT() {
    arr.sort(compareFN);
}

//comperator function
function compareFN() {
    return task.priority < task.priority;
}

function print() {
    for (var i = 0; i < arr.length; i++) {
        console.log(arr[i].priority);   
    }
}

//execution of the program
print();
sortT();
print();

EDIT: The solution is the following - As stated, the comparator function really was the problem, the correct way to write it is the following:

编辑:解决方案如下 - 如前所述,比较器功能确实是问题所在,正确的编写方法如下:

function compareFN(taskA, taskB) {
   return taskA.priority < taskB.priority;
}

回答by robbi5

The compare function needs two arguments: the first and the second element it should compare. So your compareFN should look like this:

compare 函数需要两个参数:它应该比较的第一个和第二个元素。所以你的 compareFN 应该是这样的:

function compareFN(taskA, taskB) {
   return taskA.priority - taskB.priority;
}

Edit: As NPE said, it is supposed to perform a three-way comparison, so a simple a < bis not so a great idea here.

编辑:正如 NPE 所说,它应该执行三向比较,所以在这里简单a < b不是一个好主意。

回答by NPE

There are multiple problems with your comparator:

您的比较器存在多个问题:

  1. It refers to the global taskobject instead of the objects being compared.
  2. It compares the object to itself.
  3. It is supposed to perform a three-way comparison.
  1. 它指的是全局task对象而不是被比较的对象。
  2. 它将对象与自身进行比较。
  3. 它应该执行三向比较

Try:

尝试:

var compareFN = function(a, b) {
    return a.priority - b.priority;
}

回答by dougajmcdonald

You need to change the signature of you compare function to include the two tasks.

您需要更改比较函数的签名以包含这两个任务。

For ascending order (normally what you want) you need to do b < a, a < b will do descending order

对于升序(通常是您想要的),您需要执行 b < a,a < b 将执行降序

//comperator function
function compareFN(a, b) {
    return b.priority < a.priority;
}

回答by GilbertS

The comparator function returns a negative value, zero or a positive value. Those three comprise what is called here the three-way comparison. So: ''' function cmp((a, b) => { // ascending return a - b } ''' For descending return b - a

比较器函数返回负值、零或正值。这三个构成了这里所说的三向比较。所以: ''' function cmp((a, b) => { // 升序返回 a - b } ''' 降序返回 b - a