JavaScript sort comparator function

后端 未结 4 874
梦毁少年i
梦毁少年i 2021-01-21 08:05

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 wher

4条回答
  •  甜味超标
    2021-01-21 08:56

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

    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 < b is not so a great idea here.

提交回复
热议问题