-2

I would like to sort the object indexing like array.sort

Input is

"a":{"label":"0",isEnabled":false,"status":1},
"1":{"label":"1",isEnabled":false,"status":1},
"$":{"label":"2",isEnabled":false,"status":1},
"B":{"label":"3",isEnabled":false,"status":1},    
"0":{"label":"5",isEnabled":false,"status":1},
"/":{"label":"6",isEnabled":false,"status":1}

expected output

    "$":{"label":"2",isEnabled":false,"status":1},
    "/":{"label":"6",isEnabled":false,"status":1},
    "0":{"label":"5",isEnabled":false,"status":1},
    "1":{"label":"1",isEnabled":false,"status":1},        
    "a":{"label":"0",isEnabled":false,"status":1},
    "B":{"label":"3",isEnabled":false,"status":1}

Actual result are, I understand object by default sorting with numbers, but I would like to sort like expected output as above mentioned, Any inputs please?

 "0":{"label":"5",isEnabled":false,"status":1},
    "1":{"label":"1",isEnabled":false,"status":1},
    "$":{"label":"2",isEnabled":false,"status":1},
    "/":{"label":"6",isEnabled":false,"status":1},    
    "a":{"label":"0",isEnabled":false,"status":1},
    "B":{"label":"3",isEnabled":false,"status":1}
klmuralimohan
  • 749
  • 1
  • 12
  • 31
  • These are just floating things that aren't part of any valid object. In any case, you can't "sort" object properties because they have no order. The only way you could do anything like that would be if you extracted them into an array, which you can then sort like any other array. – Herohtar May 23 '19 at 04:48
  • 1
    The order of the object keys is never guaranteed – brk May 23 '19 at 04:51
  • The order of properties in ES6 is 1) integer keys 2) other string keys in order of insertion 3) symbol keys. So, in your case 0 and 1 will always be first irrespective of what order you insert them. Please go through [this blog post](http://2ality.com/2015/10/property-traversal-order-es6.html) and [this answer](https://stackoverflow.com/a/38218582/3082296) – adiga May 23 '19 at 04:57

2 Answers2

0

Ok, object keys sorted and then used for display.

let obj = {
  "a": {
    "label": "0",
    "isEnabled": false,
    "status": 1
  },
  "1": {
    "label": "1",
    "isEnabled": false,
    "status": 1
  },
  "$": {
    "label": "2",
    "isEnabled": false,
    "status": 1
  },
  "B": {
    "label": "3",
    "isEnabled": false,
    "status": 1
  },
  "0": {
    "label": "5",
    "isEnabled": false,
    "status": 1
  },
  "/": {
    "label": "6",
    "isEnabled": false,
    "status": 1
  }
};

// Get keys in sorted order and print.
const arrKeys = Object.getOwnPropertyNames(obj).sort((a, b) => a < b);

arrKeys.forEach(k => console.log(obj[k]));
Bibberty
  • 4,282
  • 1
  • 6
  • 22
0

Since object property order is not guaranteed you next best thing is using Map. With Map the insertion order is honored and you can use it as an index:

The Map object holds key-value pairs and remembers the original insertion order of the keys.

let obj = { "a":{"label":"0",isEnabled:false,"status":1}, "1":{"label":"1",isEnabled:false,"status":1}, "$":{"label":"2",isEnabled:false,"status":1}, "B":{"label":"3",isEnabled:false,"status":1}, "0":{"label":"5",isEnabled:false,"status":1}, "/":{"label":"6",isEnabled:false,"status":1} }

let regEx = new RegExp(/[^a-zA-Z\d\s:]/)

let sortedKeys = Object.keys(obj).sort((a, b) => {
  if (regEx.test(a) && regEx.test(b))
    return a.charCodeAt(0) - b.charCodeAt(0)
  else
    return a.localeCompare(b, 'en', { numeric: true, caseFirst: 'upper'})
})

let mapped = sortedKeys.reduce((r,c) => (r.set(c, obj[c]), r), new Map())

console.log('keys: ', Array.from(mapped.keys()))
console.log('values: ', Array.from(mapped.values()))
Akrion
  • 15,757
  • 1
  • 27
  • 43