![]() Objects.sort( ( a, b) => a.createdAt - b.createdAt) ![]() Just subtract the two date properties in the sort() callback.įor example, here's how you can sort an array of objects by the createdAt property. Similarly, sorting an array of objects by a date property is easy. const dates = [ĭates.sort( ( date1, date2) => date1 - date2) ĭates Sorting an Array of Objects by a Date Property Just subtract the two dates in the sort() callback. ![]() Sorting an array of dates is a convenient one-liner, because comparing dates in JavaScript is easy: subtracting 2 dates returns the difference between the two dates in milliseconds.
0 Comments
Leave a Reply. |