- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Sort nested array containing objects ascending and descending according to date in JavaScript
Suppose we have a JSON Object that contains a nested array like this −
const arr = { "DATA": [ { "BookingID": "9513", "DutyStart": "2016-02-11 12:00:00" }, { "BookingID": "91157307", "DutyStart": "2016-02-11 13:00:00" }, { "BookingID": "95117317", "DutyStart": "2016-02-11 13:30:00" }, { "BookingID": "957266", "DutyStart": "2016-02-12 19:15:00" }, { "BookingID": "74", "DutyStart": "2016-02-11 12:21:00" } ] };
We are required to write a JavaScript function that takes in one such object and sort the nested array according to the 'dutyStart' property in either ascending or descending order.
Example
The code for this will be −
const arr = { "DATA": [ { "BookingID": "9513", "DutyStart": "2016-02-11 12:00:00" }, { "BookingID": "91157307", "DutyStart": "2016-02-11 13:00:00" }, { "BookingID": "95117317", "DutyStart": "2016-02-11 13:30:00" }, { "BookingID": "957266", "DutyStart": "2016-02-12 19:15:00" }, { "BookingID": "74", "DutyStart": "2016-02-11 12:21:00" } ] }; const sortByDate = arr => { const sorter = (a, b) => { return new Date(a.DutyStart).getTime() - new Date(b.DutyStart).getTime(); }; arr["DATA"].sort(sorter); return arr; }; console.log(sortByDate(arr));
Output
And the output in the console will be −
{ DATA: [ { BookingID: '9513', DutyStart: '2016-02-11 12:00:00' }, { BookingID: '74', DutyStart: '2016-02-11 12:21:00' }, { BookingID: '91157307', DutyStart: '2016-02-11 13:00:00' }, { BookingID: '95117317', DutyStart: '2016-02-11 13:30:00' }, { BookingID: '957266', DutyStart: '2016-02-12 19:15:00' } ] }
- Related Articles
- Sort array according to the date property of the objects JavaScript
- Sort an array according to another array in JavaScript
- Filter an array containing objects based on another array containing objects in JavaScript
- How to sort array according to age in JavaScript?
- How to sort an array of objects based on the length of a nested array in JavaScript
- How to sort date array in JavaScript
- How to sort an array of objects containing null elements in java?
- Check if an array is descending, ascending or not sorted in JavaScript
- Python Pandas - Sort DataFrame in descending order according to the element frequency
- Group objects inside the nested array JavaScript
- Python Pandas - Sort DataFrame in ascending order according to the element frequency
- How to convert nested array pairs to objects in an array in JavaScript ?
- Sort the second array according to the elements of the first array in JavaScript
- Sort by date & time in descending order in MySQL?
- How to process JavaScript nested array and display the order of numbers according to the level upto which they are nested?

Advertisements