简体   繁体   English

如何根据javascript中嵌套数组的长度对对象数组进行排序

[英]How to sort an array of objects based on a the length of a nested array in javascript

I have an array of objects in javascript, each of which in turn has an array: 我在javascript中有一个对象数组,每个对象都有一个数组:

{
    category: [ 
        { name: "Cat1", elements : [ 
            { name: name, id: id } ] 
        },
        { name: "Cat2", elements : [ 
            { name: name, id: id },
            { name: name, id: id },
            { name: name, id: id } ] 
        }, 
        { name: "Cat3", elements : [ 
            { name: name, id: id },
            { name: name, id: id } ] 
        }
    ]
}

I would like to sort the array "category" based on the number of objects within the nested array "elements". 我想根据嵌套数组“elements”中的对象数量对数组“类别”进行排序。

For example, after sorting, the above object might look like this (descending): 例如,排序后,上面的对象可能看起来像这样(降序):

{
    category: [ 
        { name: "Cat2", elements : [ 
            { name: name, id: id },
            { name: name, id: id },
            { name: name, id: id } ] 
        }, 
        { name: "Cat3", elements : [ 
            { name: name, id: id },
            { name: name, id: id } ] 
        },
        { name: "Cat1", elements : [ 
            { name: name, id: id } ] 
        }

    ]
}

I am wondering if it is possible to accomplish this using javascript's sort() method. 我想知道是否有可能使用javascript的sort()方法完成此操作。 Any suggestions? 有什么建议?

Thanks in advance! 提前致谢!

The sort method accepts a function in which you can define how to compare two elements. sort方法接受一个函数,您可以在其中定义如何比较两个元素。 Here's the relevant documentation . 这是相关文档

compareFunction Specifies a function that defines the sort order. compareFunction指定定义排序顺序的函数。 If omitted, the array is sorted lexicographically (in dictionary order) according to the string conversion of each element. 如果省略,则根据每个元素的字符串转换按字典顺序(按字典顺序)对数组进行排序。

If compareFunction is supplied, the array elements are sorted according to the return value of the compare function. 如果提供compareFunction,则根据compare函数的返回值对数组元素进行排序。 If a and b are two elements being compared, then: 如果a和b是被比较的两个元素,那么:

  • If compareFunction(a, b) is less than 0, sort a to a lower index than b. 如果compareFunction(a,b)小于0,则将a排序为低于b的索引。

  • If compareFunction(a, b) returns 0, leave a and b unchanged with respect to each other, but sorted with respect to all different elements. 如果compareFunction(a,b)返回0,则保持a和b相对于彼此保持不变,但是对于所有不同的元素进行排序。 Note: the ECMAscript standard does not guarantee this behaviour, and thus not all browsers (eg Mozilla versions dating back to at least 2003) respect this. 注意:ECMAscript标准不保证这种行为,因此并非所有浏览器(例如可追溯到至少2003年的Mozilla版本)都尊重这一点。

  • If compareFunction(a, b) is greater than 0, sort b to a lower index than a. 如果compareFunction(a,b)大于0,则将b排序为低于a的索引。

Example code 示例代码

var sorted_categories = original.category.sort(function (one, other) {
   //a - b is 
   //   0 when elements are the same
   //  >0 when a > b
   //  <0 when a < b
   return one.elements.length - other.elements.length;
});
var category = [ 
        { name: "Cat1", elements : [ 
            { name: name, id: id } ] 
        },
        { name: "Cat2", elements : [ 
            { name: name, id: id },
            { name: name, id: id },
            { name: name, id: id } ] 
        }, 
        { name: "Cat3", elements : [ 
            { name: name, id: id },
            { name: name, id: id } ] 
        }
];

function myAbcSort(a, b){
    if(a.elements.length > b.elements.length) {
        return -1;
    } else {
        return 1;
    }
}
category.sort(myAbcSort);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM