1

I have a json response with the following data in alphabetical order,

let sheetCells =
{
  0: [
     {
      "Actual Hours": 16
      "Assigned": "Jerry"
      "Completion %": 48.2
      "Days Scheduled": 2
      "Device 1": 10
      "Device 2": 43
      "Device 3": 72
      "Device 4": 91
      "Device 5": 25
      "End Date": "2018-01-03T23:00:00.000Z"
      "Estimated Hours": 16
      "ID": "1dbk3"
      "Notes": "Note 1"
      "Parent ID": "2k59f"
      "Priority": ""
      "Start Date": "2018-01-01T23:00:00.000Z"
      "Stories": "A User"
      "Tests": "Y"
     },
     {
      //...same keys as above, different values
     }
   ]
}

but this is the DESIRED format in which I want the resulting objects in the array be sorted,

[ 
  {
   "Stories": "A User" 
   "ID": "1dbk3"
   "Parent ID": "2k59f"  
   "Completion %": 48.2 
   "Priority": "" 
   "Device 1": 10
   "Device 2": 43
   "Device 3": 72
   "Device 4": 91
   "Device 5": 25 
   "Assigned": "Jerry" 
   "Notes": "Note 1"
   "Tests": "Y"
   "Estimated Hours": 16 
   "Days Scheduled": 2 
   "Start Date": "2018-01-01T23:00:00.000Z" 
   "End Date": "2018-01-03T23:00:00.000Z" 
   "Actual Hours": 16
  },
  {
   //...same keys as above, different values
  }
 ]

Here is the solution that i've tried

let sortOrder = ['Stories', 'ID', 'Parent ID', 'Completion %', 'Priority', 'Device 1', 'Device 2', 'Device 3', 'Device 4', 'Device 5', 'Assigned', 'Notes', 'Tests', 'Estimated Hours', 'Days Scheduled', 'Start Date', 'End Date', 'Actual Hours']

let result = sheetCells[0].sort((a,b) => sortOrder.indexOf(a.Stories) > sortOrder.indexOf(b.Stories)))

but this result gets just the story key and ignores the rest, please help with a working and optimal solution.Thanks

Adebayo
  • 113
  • 1
  • 8
  • Use a ES6 map if you want sort order based on keys – Kunal Mukherjee Feb 28 '20 at 16:26
  • You might also find [Javascript - sort array based on another array](https://stackoverflow.com/q/13304543/215552) of use. – Heretic Monkey Feb 28 '20 at 16:28
  • 1
    It is an extremely bad idea to rely on object property name ordering. The ordering is defined, but it's not adaptable to your own semantics. Also note that property names that look like numbers (they're all strings in reality) are ordered by a separate rule. If you need a specific order, make an array of property names, sort it how you want, and access the object properties via the array. That will *always* work, and you're in complete control of the ordering rules. – Pointy Feb 28 '20 at 16:41

1 Answers1

1

Rather than resorting the original data, you can map it to a new sorted array by indexing it with every element in your sortOrder in order. Something like this:

let result = sortOrder.map(key => sheetCells[0][key]);

While that's a working version of your solution, based on the data structure in your example, this may be closer to what you're looking for:

let result = sheetCells[0].map(cell => sortOrder.map(key => cell[key]));
Ian
  • 4,490
  • 5
  • 32
  • 62