数组排序问题
我有如下数组,我已经按每个item的index递减排序了,现在的需求是如果index相等的情况下,按time递增排序该如何处理呢?
[{
"gname": "gname661",
"index": 9,
"time": "2016-07-26 01:54:21"
},
{
"gname": "gname600",
"index": 9,
"time": "2016-07-04 14:30:54"
},
{
"gname": "gname559",
"index": 8,
"time": "2016-07-22 17:16:32"
},
{
"gname": "gname508",
"index": 8,
"time": "2016-07-10 01:31:28"
},
{
"gname": "gname402",
"index": 7,
"time": "2016-07-12 17:34:41"
}]
5 回复
//array就是你的数组
array.sort((o, n)=>o['index'] < n ['index'] || (o['index'] == n['index'] && o['time'] > n['time']));
一楼正解
@hyj1991 3Q
@hyj1991 不客气~
这个方法还是很好用的<br><br>来自疯狂的<a href=“https://github.com/1340641314/react-cnode” target="_blank">react-cnode</a>