0

I have an array of Objects and I want to sort it customarily.

 var array = [
   {
      "1":1223,
      "3":1224,
      "2":1225,
      "some_field":"Name1",
      "row":1,
      "col":1
   },
   {
      "3":10,
      "2":11,
      "1":12,
      "some_field":"Name2",
      "row":2,
      "col":1
   }
]

I need output like this

[
   {
      "some_field":"Name1",
      "row":1,
      "col":1,
      "1":1223,
      "2":1224,
      "3":1225
   },
   {
      "some_field":"Name2",
      "row":2,
      "col":1,
      "1":10,
      "2":11,
      "3":12
   }
]

How can I sort them like this? Do I need to use the length of each index and sort in descending order?

JSG
  • 1,303
  • 2
  • 8
  • 13
  • 1
    Your array is in the same order in the output as it is in the input. It looks like you're asking how to re-order the keys within each object, but you shouldn't rely on (or care about) that. What problem are you trying to solve? – ray Nov 04 '22 at 05:50
  • You can't reorder keys of the object if they have integer keys. The integer-like keys will always be enumerated before the string like keys. You need to use a `Map` if you care about the order of keys. – adiga Nov 04 '22 at 06:22
  • Yeah, I want to reorder the array – JSG Nov 04 '22 at 09:15

0 Answers0