Created
August 5, 2020 06:12
-
-
Save zzjtnb/4e604a8b6a1becb51e011831bfcf6ebd to your computer and use it in GitHub Desktop.
JavaScript-按日期对象数组进行排序,然后按时间排序
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ```JavaScript | |
| const arr = [{id: 1, value : "value1", date: "2018-08-08", time: "15:27:17"}, | |
| {id: 2, value : "value2", date: "2018-08-09", time: "12:27:17"}, | |
| {id: 3, value : "value3", date: "2018-08-10", time: "17:27:17"}, | |
| {id: 4, value : "value4", date: "2018-08-10", time: "01:27:17"}, | |
| {id: 5, value : "value5", date: "2018-08-10", time: "09:27:17"}, | |
| {id: 6, value : "value6", date: "2018-08-10", time: "23:27:17"}, | |
| {id: 7, value : "value7", date: "2018-08-10", time: "16:27:17"}, | |
| {id: 8, value : "value8", date: "2018-08-11", time: "10:27:17"} | |
| ]; | |
| arr.sort((a, b) => a.date.localeCompare(b.date) || a.time.localeCompare(b.time)); | |
| console.log(arr); | |
| ``` | |
| 若要排序为降序,只需切换as和bs: | |
| ```JavaScript | |
| const arr = [{id: 1, value : "value1", date: "2018-08-08", time: "15:27:17"}, | |
| {id: 2, value : "value2", date: "2018-08-09", time: "12:27:17"}, | |
| {id: 3, value : "value3", date: "2018-08-10", time: "17:27:17"}, | |
| {id: 4, value : "value4", date: "2018-08-10", time: "01:27:17"}, | |
| {id: 5, value : "value5", date: "2018-08-10", time: "09:27:17"}, | |
| {id: 6, value : "value6", date: "2018-08-10", time: "23:27:17"}, | |
| {id: 7, value : "value7", date: "2018-08-10", time: "16:27:17"}, | |
| {id: 8, value : "value8", date: "2018-08-11", time: "10:27:17"} | |
| ]; | |
| arr.sort((a, b) => b.date.localeCompare(a.date) || b.time.localeCompare(a.time)); | |
| console.log(arr); | |
| ``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment