Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
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 need to write a JavaScript function that sorts the nested array according to the 'DutyStart' property in either ascending or descending order.
Sorting in Ascending Order
The following function sorts the array by date in ascending order (earliest to latest):
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 sortByDateAscending = (obj) => {
const sorter = (a, b) => {
return new Date(a.DutyStart).getTime() - new Date(b.DutyStart).getTime();
};
obj["DATA"].sort(sorter);
return obj;
};
console.log(sortByDateAscending(arr));
{
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' }
]
}
Sorting in Descending Order
To sort in descending order (latest to earliest), we reverse the comparison:
const arr2 = {
"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 sortByDateDescending = (obj) => {
const sorter = (a, b) => {
return new Date(b.DutyStart).getTime() - new Date(a.DutyStart).getTime();
};
obj["DATA"].sort(sorter);
return obj;
};
console.log(sortByDateDescending(arr2));
{
DATA: [
{ BookingID: '957266', DutyStart: '2016-02-12 19:15:00' },
{ BookingID: '95117317', DutyStart: '2016-02-11 13:30:00' },
{ BookingID: '91157307', DutyStart: '2016-02-11 13:00:00' },
{ BookingID: '74', DutyStart: '2016-02-11 12:21:00' },
{ BookingID: '9513', DutyStart: '2016-02-11 12:00:00' }
]
}
Generic Function with Order Parameter
For better reusability, we can create a generic function that accepts an order parameter:
const sortByDate = (obj, order = 'asc') => {
const sorter = (a, b) => {
const dateA = new Date(a.DutyStart).getTime();
const dateB = new Date(b.DutyStart).getTime();
if (order === 'desc') {
return dateB - dateA;
}
return dateA - dateB;
};
obj["DATA"].sort(sorter);
return obj;
};
// Test with ascending order
const testData = {
"DATA": [
{"BookingID": "1", "DutyStart": "2016-02-11 15:00:00"},
{"BookingID": "2", "DutyStart": "2016-02-11 10:00:00"},
{"BookingID": "3", "DutyStart": "2016-02-12 08:00:00"}
]
};
console.log("Ascending:", sortByDate(JSON.parse(JSON.stringify(testData)), 'asc'));
console.log("Descending:", sortByDate(JSON.parse(JSON.stringify(testData)), 'desc'));
Ascending: {
DATA: [
{ BookingID: '2', DutyStart: '2016-02-11 10:00:00' },
{ BookingID: '1', DutyStart: '2016-02-11 15:00:00' },
{ BookingID: '3', DutyStart: '2016-02-12 08:00:00' }
]
}
Descending: {
DATA: [
{ BookingID: '3', DutyStart: '2016-02-12 08:00:00' },
{ BookingID: '1', DutyStart: '2016-02-11 15:00:00' },
{ BookingID: '2', DutyStart: '2016-02-11 10:00:00' }
]
}
Key Points
- Use
new Date()constructor to convert date strings to Date objects - Compare using
getTime()method for accurate sorting - For ascending:
dateA - dateB - For descending:
dateB - dateA - The
sort()method modifies the original array
Conclusion
Sorting nested arrays by date involves converting date strings to Date objects and comparing their timestamps. Use ascending or descending comparison functions based on your sorting requirements.
Advertisements
