Javascript通过created_date(desc)在对象数组的数组中对对象进行排序

时间:2018-04-18 13:52:41

标签: javascript arrays sorting

这是一个过于简化的示例,但我很好奇如何按日期顺序返回对象(从最新到最旧)而不更改数组顺序:

let arrayOfArrays = [
    array1 = [
        object1 = {id: 1, name: 'object1', created_date: '2012-01-15'},
        object2 = {id: 2, name: 'object2', created_date: '2018-01-15'},
        object3 = {id: 3, name: 'object3', created_date: '2018-01-17'}
    ],
    array2 = [
        object4 = {... created_date: '2013-01-15'},
        object5 = {... created_date: '2017-01-15'},
        ...and so on...
    ],
    ...more arrays...
]

这是我正在尝试排序的实际图像: enter image description here

1 个答案:

答案 0 :(得分:0)

只需迭代外部数组,取出每个内部数组并对其进行排序:

 for(const array of arrayOfArrays) {
   array.sort(/*...*/);
 }

您可以详细了解排序的工作原理here