如何按日期对javascript对象数组进行排序

时间:2021-08-27 15:13:40

I am trying to sort an array of objects with each object containing

我正在尝试对每个对象包含的对象进行排序。

var recent = [{id: "123",age :12,start: "10/17/13 13:07"} , {id: "13",age :62,start: "07/30/13 16:30"}];

Date format is: mm/dd/yy hh:mm

日期格式为:mm /dd/yy hh:mm

I want to sort in order of date with the most recent first. If date is same it should be sorted on time.

我想按最近的第一个排序。如果日期相同,则应及时排序。

I tried out the below sort function. But it is not working.

我尝试了下面的排序函数。但这并不奏效。

recent.sort(function(a,b))
{
    a = new Date(a.start);
    b = new Date(b.start);
    return a-b;
});

Also how should I iterate through the objects for sorting? Something like:

我还应该如何遍历对象进行排序?喜欢的东西:

for (var i = 0; i < recent.length; i++)
    {
        recent[i].start.sort(function (a, b)
        {
            a = new Date(a.start);
            b = new Date(b.start);
            return a-b; 
        } );
    }

There can be any number of objects in the array.

数组中可以有任意数量的对象。

3 个解决方案

#1


53  

As has been pointed out in the comments, the definition of recent isn't correct javascript.

正如评论中指出的,近期的定义是不正确的javascript。

But assuming the dates are strings:

但假设日期是字符串:

var recent = [
    {id: 123,age :12,start: "10/17/13 13:07"}, 
    {id: 13,age :62,start: "07/30/13 16:30"}
];

then sort like this:

然后排序是这样的:

recent.sort(function(a,b) { 
    return new Date(a.start).getTime() - new Date(b.start).getTime() 
});

More details on sort function from W3Schools

更多关于W3Schools的分类功能的细节

#2


5  

recent.sort(function(a,b) { return new Date(a.start).getTime() - new Date(b.start).getTime() } );

#3


0  

This function allows you to create a comparator that will walk a path to the key you would like to compare on:

此功能允许您创建一个比较器,该比较器将沿着路径找到您想要比较的键:

function createDateComparator ( path = [] , comparator = (a, b) => a.getTime() - b.getTime()) {
  return (a, b) => {
    let _a = a
    let _b = b
    for(let key of path) {
      _a = _a[key]
      _b = _b[key]
    }
    return comparator(_a, _b)
  }
}


const input = (
  [ { foo: new Date(2017, 0, 1) }
  , { foo: new Date(2018, 0, 1) }
  , { foo: new Date(2016, 0, 1) }
  ]
)

const result = input.sort(createDateComparator([ 'foo' ]))

console.info(result)

#1


53  

As has been pointed out in the comments, the definition of recent isn't correct javascript.

正如评论中指出的,近期的定义是不正确的javascript。

But assuming the dates are strings:

但假设日期是字符串:

var recent = [
    {id: 123,age :12,start: "10/17/13 13:07"}, 
    {id: 13,age :62,start: "07/30/13 16:30"}
];

then sort like this:

然后排序是这样的:

recent.sort(function(a,b) { 
    return new Date(a.start).getTime() - new Date(b.start).getTime() 
});

More details on sort function from W3Schools

更多关于W3Schools的分类功能的细节

#2


5  

recent.sort(function(a,b) { return new Date(a.start).getTime() - new Date(b.start).getTime() } );

#3


0  

This function allows you to create a comparator that will walk a path to the key you would like to compare on:

此功能允许您创建一个比较器,该比较器将沿着路径找到您想要比较的键:

function createDateComparator ( path = [] , comparator = (a, b) => a.getTime() - b.getTime()) {
  return (a, b) => {
    let _a = a
    let _b = b
    for(let key of path) {
      _a = _a[key]
      _b = _b[key]
    }
    return comparator(_a, _b)
  }
}


const input = (
  [ { foo: new Date(2017, 0, 1) }
  , { foo: new Date(2018, 0, 1) }
  , { foo: new Date(2016, 0, 1) }
  ]
)

const result = input.sort(createDateComparator([ 'foo' ]))

console.info(result)