Context
Need to sort following Objects by readingDate
using lodash, I'm able to sort it but its indexes are not preserved.
Original JSON Object
{
"Inventory Creation": [
{
"id": 150,
"reading": "12345",
"readingDate": "2020-07-14"
}
],
"Inventory & Check-in": [
{
"id": 151,
"reading": "12345",
"readingDate": "2020-11-14"
}
],
"Check-in": [
{
"id": 152,
"reading": "12345",
"readingDate": "2020-08-14"
}
]
}
Code Which I've Tried
_.sortBy(unsorted, o => o[0].readingDate).reverse();
here unsorted
contains the above Orignal JSON object
Actual Result (What I'm getting)
{
0: [
{
"id": 151,
"reading": "12345",
"readingDate": "2020-11-14"
}
],
1: [
{
"id": 152,
"reading": "12345",
"readingDate": "2020-08-14"
}
],
2: [
{
"id": 150,
"reading": "12345",
"readingDate": "2020-07-14"
}
]
}
Expected Result (What I want)
{
"Inventory & Check-in": [
{
"id": 151,
"reading": "12345",
"readingDate": "2020-11-14"
}
],
"Check-in": [
{
"id": 152,
"reading": "12345",
"readingDate": "2020-08-14"
}
],
"Inventory Creation": [
{
"id": 150,
"reading": "12345",
"readingDate": "2020-07-14"
}
]
}