0
var obj = '{"2": "Apple","3": "Banana","4": "Orange","6": "Grapes","1": "Kiwi","7":"Melon","5": "Mango"}';
 
var parsed = $.parseJSON(obj); 
 
console.log(parsed);

it returns this sorted obj by key:

{
  1: "Kiwi",
  2: "Apple",
  3: "Banana",
  4: "Orange",
  5: "Mango",
  6: "Grapes",
  7: "Melon"
}

how to retain the original sort?

woninana
  • 3,409
  • 9
  • 42
  • 66
  • The sort order of properties in an object is not guaranteed. If you need it to be, use an array instead. – Rory McCrossan Jun 22 '22 at 17:30
  • 1
    the order is actually: all positive 32 bit integer values, like indices first, then all other in insertation order, followed by symbols. – Nina Scholz Jun 22 '22 at 17:32
  • 1
    @NinaScholz that's true for Chromium based browsers, but not all browsers. Which is why it can't be guaranteed in all situations. An array solves any ambiguity in the browser's implementation of property sorting. – Rory McCrossan Jun 22 '22 at 17:35

0 Answers0