如何排序的基础上在JavaScript中嵌套数组的长度对象的数组 [英] How to sort an array of objects based on a the length of a nested array in javascript

查看:168
本文介绍了如何排序的基础上在JavaScript中嵌套数组的长度对象的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在JavaScript对象,每个反过来又一个数组的数组:

I have an array of objects in javascript, each of which in turn has an array:

{
    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".

例如,排序后,上述目标可能会是这样(降序):

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 } ] 
        }

    ]
}

我想知道是否有可能做到这一点使用JavaScript的sort()方法。有什么建议?

I am wondering if it is possible to accomplish this using javascript's sort() method. Any suggestions?

在此先感谢!

推荐答案

排序方法接受在其中您可以定义如何比较两个元素的函数。这里的相关文件

The sort method accepts a function in which you can define how to compare two elements. Here's the relevant documentation.

的compareFunction 指定
  函数定义排序顺序。
  如果省略,数组排序
  字典顺序(在字典
  根据字符串顺序)
  转换的每个元素。

compareFunction Specifies a function that defines the sort order. If omitted, the array is sorted lexicographically (in dictionary order) according to the string conversion of each element.

如果的compareFunction被提供时,所述阵列元件是按照比较函数的返回值排序。如果a和b是两个元件被比较,则:

If compareFunction is supplied, the array elements are sorted according to the return value of the compare function. If a and b are two elements being compared, then:


  • 如果的compareFunction(A,B)小于0,排序一个比B A较低折射率

  • If compareFunction(a, b) is less than 0, sort a to a lower index than b.

如果的compareFunction(A,B)返回0,离开相对于彼此和b不变,但相对于全部不同的元素进行排序。注:ECMAScript标准并不能保证这种行为,因此不是所有的浏览器(例如Mozilla的版本可以追溯到至少2003年)尊重这个

If compareFunction(a, b) returns 0, leave a and b unchanged with respect to each other, but sorted with respect to all different elements. Note: the ECMAscript standard does not guarantee this behaviour, and thus not all browsers (e.g. Mozilla versions dating back to at least 2003) respect this.

如果的compareFunction(A,B)大于0,排序b键比较低折射率

If compareFunction(a, b) is greater than 0, sort b to a lower index than a.

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;
});

这篇关于如何排序的基础上在JavaScript中嵌套数组的长度对象的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆