按日期键对 JSON 数组进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16574309/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sort JSON array by date key
提问by jQuerybeast
I have a json array and I am trying to sort it by datebefore I append it.
我有一个 json 数组,我试图在附加它之前按日期对其进行排序。
The array looks as such:
该数组如下所示:
result = [];
result.push(
{id: 'ID', result: {url: 'test', date: '15 May 2013, 6:40 pm'}},
{id: 'ID', result: {url: 'test', date: '20 Dec 2012, 8:00 am'}},
{id: 'ID', result: {url: 'test', date: '29 Jun 2012, 5:47 pm'}}
);
Currently, I have managed to sort an arrayof dates as such:
目前,我已经设法对一组日期进行排序:
var datearray = [
'2011-05-26 12:00:00',
'2016-01-26 12:00:00',
'2011-01-26 12:00:00',
'2012-12-08 07:00:00',
'2011-01-26 12:00:00',
'1995-01-05 06:00:00'
];
datearray.sort();
which gives me:
这给了我:
1995-01-05 06:00:00
2011-01-26 12:00:00
2011-01-26 12:00:00
2011-05-26 12:00:00
2012-12-08 07:00:00
2016-01-26 12:00:00
but Im not sure how to do the same for a complex arraywhich contains more keys than one. I know that I should firstly format the date to YYYY-MM-DDbut after im kinda messed up.
但我不确定如何对包含多个键的复杂数组执行相同的操作。我知道我应该首先将日期格式化为 YYYY-MM-DD但之后我有点搞砸了。
A good start would be from what I currently came up with found on jsbin: http://jsbin.com/ipatok/8/edit
一个好的开始是我目前在 jsbin 上发现的:http://jsbin.com/ipatok/8/edit

