Dev./javascript
[JS] Date 역순 정렬
인쥭
2022. 5. 17. 14:29
반응형
const date = new Date();
const day = date.getDate();
const date2 = new Date();
date2.setDate(day + 5);
const date3 = new Date();
date3.setDate(day + 3)
const date4 = new Date();
const arr = [date2, date4, date, date3];
console.log(
arr.sort(
(a, b) => b - a
// 오름차순 정렬은 (a, b) => a - b
)
);
/* 실행 결과
[
2022-05-22T05:28:29.877Z,
2022-05-20T05:28:29.877Z,
2022-05-17T05:28:29.877Z,
2022-05-17T05:28:29.877Z
]
*/