-2

I m having following dictionary object format.

var cat_score = {"apr": [{"aaa": 1}, {"aaa1": 0}], "mar": [{"aaa": 1}, {"aaa1": 0}]}

i need output like

var cat_score = { "mar": [{"aaa": 1}, {"aaa1": 0}],"apr": [{"aaa": 1}, {"aaa1": 0}]}

how to sort json object based on key using js I have to sort following list order

order = ['jan','feb','mar','apr']
prasanth
  • 22,145
  • 4
  • 29
  • 53
Sai Rajesh
  • 1,882
  • 5
  • 22
  • 41

1 Answers1

2

There is no JSON in your question, but JavaScript objects. These objects are defined as an unordered collection of properties. Most (all?) ECMAScript implementations keep the order, but I would not count on that.

Instead you could use a Map which guarantees to iterate through the properties in insertion-order. So you can create a new Map and use the link of Dinesh to insert the arrays in order into the Map.

str
  • 42,689
  • 17
  • 109
  • 127