我有一个数组。我必须按给定的注册日期以相反的顺序对数组进行排序。
下面是我编写的函数
getDevicesList(devices, user) {
let activeDevices = [];
const pendingDevices = [];
const historyDevices = [];
devices && devices.forEach((device) => {
if(device.get("status") === DEVICES_STATUS.INACTIVE && !UserUtils.isStatusArchived(user.get("status"))) {
pendingDevices.push(device);
} else if(device.get("isRecentlyUsed")) {
activeDevices.push(device);
} else {
historyDevices.push(device);
}
});
var sorted_meetings = historyDevices.sort((a,b) => new Date(a.enrolledDate) < new Date(b.enrolledDate));
console.log(sorted_meetings);
activeDevices = [...pendingDevices, ...activeDevices];
const noDevices = activeDevices.length === 0 && historyDevices.length === 0;
return { activeDevices, historyDevices, noDevices };
}
我试图将JSON日期转换为正常日期。但这对我没有用。
var sorted_meetings = historyDevices.sort((a,b) => {
var dateA = moment(a.enrolledDate).format("dddd, MMMM Do YYYY, h:mm:ss a");
var dateB = moment(b.enrolledDate).format("dddd, MMMM Do YYYY, h:mm:ss a");
return new Date(dateA).getTime() - new Date(dateB).getTime()
}).reverse();
这是我必须排序的数组。
historyDevices : (4) [Map, Map, Map, Map]
0: Map
size: 8
__altered: false
__hash: undefined
__ownerID: undefined
_root: ArrayMapNode
entries: Array(8)
0: (2) ["id", 81054566]
1: (2) ["make", "iPhone"]
2: (2) ["model", 5]
3: (2) ["status", "ACTIVE"]
4: (2) ["nickName", "abc abc"]
5: (2) ["lastUsedDate", "2018-04-13T18:41:59.193Z"]
6: (2) ["enrolledDate", "2019-03-23T03:54:19.620Z"]
7: (2) ["isRecentlyUsed", false]
length: 8
__proto__: Array(0)
ownerID: OwnerID {}
__proto__: Object
__proto__: KeyedIterable
1: Map {size: 8, _root: ArrayMapNode, __ownerID: undefined, __hash: undefined, __altered: false}
2: Map {size: 8, _root: ArrayMapNode, __ownerID: undefined, __hash: undefined, __altered: false}
3: Map {size: 8, _root: ArrayMapNode, __ownerID: undefined, __hash: undefined, __altered: false}
length: 4
__proto__: Array(0)
结果:我希望所有的数组元素都应该按相反的enrolledDate顺序出现。
答案 0 :(得分:0)
如何?
var sorted_meetings = historyDevices.sort((a,b) => new Date(b.enrolledDate) - new Date(a.enrolledDate));