0

I have the following Object:

const object = {
  "9c8vDTruRDPi937aHMMz":{
    name: "item1",
    createdAt: {
      seconds: 1582227276, 
      nanoseconds: 68000000
    }
  },
  "rAexYu2rAz0AEnD77x5p":{
    name: "item2",
    createdAt: {
      seconds: 1582227577,
      nanoseconds: 922000000
    }
  }
}

And would like to sort it depending on createdAt.seconds and keeping it as an object.

For example, the return value would be (if sorting depending on seconds and Desc):

const sortedByDateObject = {
  "rAexQu3rdm0AEnD77x5p":{
    name: "item2",
    createdAt: {
      seconds: 1582227577,
      nanoseconds: 922000000
    }
  },
  "9c8vDGryRSZi817aHMMz":{
    name: "item1",
    createdAt: {
      seconds: 1582227276, 
      nanoseconds: 68000000
    }
  }
}

Know how to sort arrays with lodash, but not this kind of complex task with nested objects.

Any help is much appreaciated :)

Albert
  • 39
  • 3
  • 1
    [Objects do not guarantee the order of properties](https://stackoverflow.com/a/5525820/7328218) – Taki Feb 20 '20 at 20:43
  • exactly, you should use an array of objects, `[{key: value}, {key: value}]`, in this case the order is guaranted, and I also don't think you can sort in an object at all. – Sándor Bakos Feb 20 '20 at 20:45
  • Thanks for the answers! How would I be able to convert to an array and sort depending on value nested object? – Albert Feb 20 '20 at 22:54

1 Answers1

0

This will turn your object into an array that can be properly sorted (as pointed out by @Taki, object property order is not guaranteed).

const object = {
  "9c8vDTruRDPi937aHMMz":{
    name: "item1",
    createdAt: {
      seconds: 1582227276, 
      nanoseconds: 68000000
    }
  },
  "rAexYu2rAz0AEnD77x5p":{
    name: "item2",
    createdAt: {
      seconds: 1582227577,
      nanoseconds: 922000000
    }
  }
};

const orderedArray = _.chain(object)
  .toPairs()
  .map(([key, ...obj]) => ({
    key,
    ...obj[0],
    createdAtSeconds: obj[0].createdAt.seconds
  }))
  .orderBy(['createdAtSeconds'], ['desc'])
  .map(({ createdAtSeconds, ...obj }) => obj)
  .value()
  
console.log(orderedArray);
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.15/lodash.min.js"></script>
th3n3wguy
  • 3,166
  • 2
  • 20
  • 28