-2

I need to sort the keys to an exact order, for example I have the next array:

const arr = [
             {
               active: true,
               code: "1234",
               indentifier: "OPC-999",
               name: "Example",
               start: "02-12-1997",
             }
             {
               active: false,
               code: "4567",
               indentifier: "OPC-111",
               name: "Example",
               start: "03-02-2010",
             }
]

The exact order for every object should be: name, indentifier, code, start, active.

I don't need to sort by value but by key name.

What's the best way to do this?

Mat Mol
  • 45
  • 8
  • Why do you need them to be in a specific order? – Adriano Mar 08 '21 at 22:33
  • The component I'm trying to use needs me to put them in order – Mat Mol Mar 08 '21 at 22:34
  • I don't think objects have a sense of order. – Julia Mar 08 '21 at 22:36
  • Can you share the component's code, then? – Adriano Mar 08 '21 at 22:36
  • Does this answer your question? [Sort JavaScript object by key](https://stackoverflow.com/questions/5467129/sort-javascript-object-by-key) – Heretic Monkey Mar 08 '21 at 22:36
  • No, I can't share the component code and @HereticMonkey no because that would be sorting by alphabetical order. Thanks nonetheless. – Mat Mol Mar 08 '21 at 22:40
  • So, how do you want the items to be sorted? I am starting to think you will need a list rather than an array. – Adriano Mar 08 '21 at 22:41
  • 1
    Then [Javascript sort an object by keys based on another Array?](https://stackoverflow.com/q/35971456/215552) since apparently copy-paste-ability is key... – Heretic Monkey Mar 08 '21 at 22:45
  • Why not hold a separate list of keys with the order? Alternatively, you could rebuild it as a list & failsafe the properties that are not included. But honestly, that would be flat out overkill: https://jsfiddle.net/mbfkn5g2/ – Jens Ingels Mar 08 '21 at 23:00

2 Answers2

1

You can create a keys array with the object keys in your desired order. Then you can map each object in the array, generating a new object with properties created in the order defined in the keys array:

const arr = [
             {
               active: true,
               code: "1234",
               indentifier: "OPC-999",
               name: "Example",
               start: "02-12-1997",
             },
             {
               active: false,
               code: "4567",
               indentifier: "OPC-111",
               name: "Example",
               start: "03-02-2010",
             }
]

const keys = ['name', 'indentifier', 'code', 'start', 'active'];

const result = arr.map(o => Object.fromEntries(keys.map(k => [k, o[k]])));

console.log(result);
Nick
  • 118,076
  • 20
  • 42
  • 73
1
for (i = 0; i < arr.length; i++) {
  arr[i] = {
    name: arr[i].name,
    indentifier: arr[i].indentifier,
    code: arr[i].code,
    start: arr[i].start,
    active: arr[i].active,
  }
}

After doing this, your arr will be sorted as you wanted:

[
  {
    "name": "Example",
    "indentifier": "OPC-999",
    "code": "1234",
    "start": "02-12-1997",
    "active": true
  },
  {
    "name": "Example",
    "indentifier": "OPC-111",
    "code": "4567",
    "start": "03-02-2010",
    "active": false
  }
]
Vivek Maharajh
  • 6,166
  • 3
  • 20
  • 27