Background
Using JavaScript, I need to sort a large JSON object based on a given property of that object. I am assuming that a merge sort is the fastest approach. If this is not the fastest approach, please tell me what is. There are myriad examples online of a merge sort against an array, but very little with objects. Here is a sample object:
fruitForSale = {
1: {"type":"orange","UnitPrice":0.20},
2: {"type":"banana","UnitPrice":0.30},
3: {"type":"pear","UnitPrice":0.10},
4: {"type":"apple","UnitPrice":0.50},
5: {"type":"peach","UnitPrice":0.70}
}
Question
Using a merge sort (or faster algorithm), how would I sort the fruitForSale
object so that I end up with an object sorted by 'type':
fruitForSale = {
4: {"type":"apple","UnitPrice":0.50},
2: {"type":"banana","UnitPrice":0.30},
1: {"type":"orange","UnitPrice":0.20},
5: {"type":"peach","UnitPrice":0.70},
3: {"type":"pear","UnitPrice":0.10}
}
NOTE: The original keys
(1,2,3,4 & 5) would need to stay assigned to their respective object, so a key of 1
should always match with {"type":"orange","UnitPrice":0.20}
and a key of 2
will always match with {"type":"banana","UnitPrice":0.30}
and so on.
Thanks!