I have an object of nested objects and would like to sort by one of the nested values (e.g. "month_sales" descending). This is what the data looks like:
{
"Bob": { sales: 13, revenue: 33, month_sales: 149 },
"Bill": { today_sales: 20, week_sales: 38, month_sales: 186 },
"Jane": { today_sales: 17, week_sales: 38, month_sales: 164 }
}
This is what I want it to look like (sort by month_sales descending):
{
"Bill": { today_sales: 20, week_sales: 38, month_sales: 186 },
"Jane": { today_sales: 17, week_sales: 38, month_sales: 164 }
"Bob": { sales: 13, revenue: 33, month_sales: 149 },
}
I have looked all over Google and StackOverflow, and have only found a couple answers that deal with sorting an object rather than an array, and in every case the answer has been along the lines of "write a function to convert the object to an array, sort that, and then turn it back into an object."
I'm willing to do that as a last resort, but it seems like I should be able to do this without deconstructing the entire object and putting it back together again. Maybe there's an elegant way to do this in lodash or something?