1

I have a collection of data that looks like this:

const  data = {
    5c80dd171d9e7b3755911818: {
      "_id": "5c80dd171d9e7b3755911818",
      "created_at": "2019-03-07T08:25:11.323Z",
      "description": ""
    }
    5c80dd1b1d9e7b3755911819: {
      "_id": "5c80dd1b1d9e7b3755911819",
      "created_at": "2019-03-07T08:24:11.323Z",
      "description": ""
    }
    5c80dd231d9e7b375591181a: {
      "_id": "5c80dd231d9e7b375591181a",
      "created_at": "2019-03-07T08:27:11.323Z",
      "description": ""
    }
}

I've been trying to transform data to an array, sort items inside and then using _keyBy from loadash to create collection once again - but it returns object in mixed order.

Is there a way that I can sort this collection by the created_at value inside an object and get back the same collection and not an array, but sorted ??

p7adams
  • 622
  • 1
  • 9
  • 24
  • Possible, but very weird to do (and the sorting order will not be guaranteed in ES5 or lower environments). If you want an ordered collection, best to use an array instead – CertainPerformance Mar 07 '19 at 09:11
  • This will do the trick: `Object.values(data).sort((a,b)=>a.created_at.localeCompare(b.created_at))` – HMR Mar 07 '19 at 09:40

0 Answers0