Sorting Array using JavaScript

This question already has an answer here:

  • Sort array of objects by string property value in JavaScript 33 answers

  • JavaScript's Array#sort accepts a function that it will call repeatedly with pairs of entries from the array. The function should return 0 if the elements are equivalent, <0 if the first element is "less than" the second, or >0 if the first element is "greater than" the second. So:

    Con.sort(function(a, b) {
        if (a.Team === b.Team) {
            return 0;
        }
        return a.Team < b.Team ? -1 : 1;
    });
    

    You can do that on one line if you're into that sort of thing (I find it easier to debug if I don't):

    Con.sort(function(a, b) { return a.Team === b.Team ? 0 : a.Team < b.Team ? -1 : 1; });
    
    链接地址: http://www.djcxy.com/p/19344.html

    上一篇: Javascript:以编程方式排序对象的数组

    下一篇: 使用JavaScript对数组进行排序