JavaScript sort comparator function

后端 未结 4 841
梦毁少年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:44

    There are multiple problems with your comparator:

    1. It refers to the global task object instead of the objects being compared.
    2. It compares the object to itself.
    3. It is supposed to perform a three-way comparison.

    Try:

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

提交回复
热议问题