javascript sort and sort equals on result. how?

This question already has an answer here:

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

  • 为了让你排序稳定,你需要比较“相等”项目的索引:

    // mass = [{name:…, count:…}, {name:…, count:…}, …]
    for (var i=0; i<mass.length; i++)
        mass[i].index = i;
    mass.sort(function(a, b) {
        return compareElements(a, b) || a.index - b.index;
    });
    function compareElements(a, b) {
        // something
        return a.count - b.count;
    }
    

    If your objects are like:

    var arrObj = {
       name: "aaa",
       count: 1
    };
    

    Use this function for comparing:

    function compareElements(a, b)
    {
      return a.count - b.count;
    }
    
    链接地址: http://www.djcxy.com/p/19330.html

    上一篇: 通过字符串属性排序对象的数组

    下一篇: JavaScript排序和排序等于结果。 怎么样?