JS对对象进行排序的问题?

小程序 文章 2020-07-31 20:41 516 0 全屏看文

AI助手支持GPT4.0

JS对对象进行排序的问题?

JS,有个数组对象,结构大概是这样的,如果我要根据currtentTime对对象进行排序(降序),怎样写比较好。

我尝试用sort() ,但没有成功。请指教,谢谢

[{  content: '内容内容',  currtentTime: 1596184337513}, {  content: '内容内容',  currtentTime: 1596994339913}, {  content: '内容内容',  currtentTime: 1596884377513}]

JS, there is an array object. The structure is like this. If I want to sort the objects according to the currenttime (descending order), how to write it better. I tried to use sort (), but it didn't work. Thank you for your advice [{content: 'content', currenttime: 15961843377513}, {content: 'content', currenttime: 1596994339913}, {content: 'content', currenttime: 1596884377513}]

回答:

Candyland丶:
[  {    content: "内容内容",    currtentTime: 1596184337513,  },  {    content: "内容内容",    currtentTime: 1596994339913,  },  {    content: "内容内容",    currtentTime: 1596884377513,  },].sort((before, after) => after.currtentTime - before.currtentTime)
鲤子:
let list = [{  content: '内容内容',  currtentTime: 1596184337513}, {  content: '内容内容',  currtentTime: 1596994339913}, {  content: '内容内容',  currtentTime: 1596884377513}]list.sort((a, b) => b.currtentTime - a.currtentTime)

xplee:

这样写不行?

myArray.sort((a, b) => {  return b.currtentTime - a.currtentTime;})cconsole.log(myArray)

-EOF-

AI助手支持GPT4.0