最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Javascript - Sort array of objects by date then by time - Stack Overflow

matteradmin2PV0评论

I have the following array:

[{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-11", time: "10:27:17"}]

How can I go about sorting the array from earliest to latest or vice versa?

I tried sorting by date, but sorting it by time swaps the order of record id 4 to id 3, because it has an earlier time value than record 3, but is technically by definition, later.

Given this array and json structure, how do I sort the array to take both fields (date and time) into consideration?

I have the following array:

[{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-11", time: "10:27:17"}]

How can I go about sorting the array from earliest to latest or vice versa?

I tried sorting by date, but sorting it by time swaps the order of record id 4 to id 3, because it has an earlier time value than record 3, but is technically by definition, later.

Given this array and json structure, how do I sort the array to take both fields (date and time) into consideration?

Share Improve this question edited Aug 11, 2018 at 0:04 Greg Venech 9,1902 gold badges22 silver badges30 bronze badges asked Aug 9, 2018 at 2:28 muffinmuffin 2,10410 gold badges46 silver badges81 bronze badges 2
  • Possible duplicate of How to sort date strings (format exmaple: 2014 7 23) in JavaScript? – Blue Commented Aug 9, 2018 at 2:32
  • hello, the answer does not seem to consider a 2nd property time that is separate from date. – muffin Commented Aug 9, 2018 at 2:54
Add a ment  | 

1 Answer 1

Reset to default 7

Sort by the difference in the dates, and if there is no difference, sort by the difference in the times, in a single .sort function:

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);

The difference in dates will be returned, except if they're the same, in which case the localCompare will e out to 0, and the difference in times will be returned instead.

To sort to descending instead, just switch the as and bs:

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);

Post a comment

comment list (0)

  1. No comments so far