2

How can I sort this type of object?

const obj = [{
  "02/10/2021": 291692.14,
  "08/12/2023": 140908.90579999998,
  "09/04/2021": 14.4776,
  "09/06/2023": 863.8189,
  "10/06/2022": 388.9343999999999,
  "10/09/2021": 187.7662,
  "10/12/2021": -85216.4669,
}, {
  "02/10/2021": 189423.30190000002,
  "08/12/2023": 122105.0051,
  "09/04/2021": -0.4682,
  "09/06/2023": 532.7351,
  "10/06/2022": 219.33960000000002,
  "10/09/2021": 581.5980999999999,
  "10/12/2021": -744.8197,
}, ]

As you can read, the keys are date strings, I need to sort the list of objects inside by these keys. e.g:

before sort : {
              "02/10/2021": 291692.14,
              "08/12/2023": 140908.90579999998,
              "09/04/2021": 14.4776,
              "09/06/2023": 863.8189,
              "10/06/2022": 388.9343999999999,
              "10/09/2021": 187.7662,
              "10/12/2021": -85216.4669,
           }

after sort : {
              "09/04/2021": 14.4776,
              "02/10/2021": 291692.14,
              "10/12/2021": -85216.4669,
              "10/06/2022": 388.9343999999999,
              "09/06/2023": 863.8189,
              "08/12/2023": 140908.90579999998,           
           }

I tried a lot of example like this or this one:

(obj).map((val) => {
    Object.entries(val).sort((a, b) => {
      const aa = a[0].split('/').reverse().join();
      const bb = b[0].split('/').reverse().join();
      return aa < bb ? -1 : (aa > bb ? 1 : 0);
    });
  });
zmag
  • 6,257
  • 12
  • 26
  • 33
  • Your object keys (i.e.: `02/10/2021`) are invalid strings. Did you forget to add `""` quotes ? Also, it's impossible to sort objects. – kemicofa ghost Apr 19 '21 at 08:08
  • yes I forgot them. Gonna edit – Salvatore Difranco Apr 19 '21 at 08:10
  • It is an object how can you sort in an object. for sorting there must be an array – decpk Apr 19 '21 at 08:10
  • It this data in `dd/mm/yyyy` or `mm/dd/yyyy` – decpk Apr 19 '21 at 08:14
  • @decpk It's `dd/mm/yyyy`. `"08/12/2023"` comes after `"09/06/2023"`. – jabaa Apr 19 '21 at 08:16
  • You should read [Sort JavaScript object by key](https://stackoverflow.com/questions/5467129/sort-javascript-object-by-key). TLDR: The order of properties in JavaScript is specified and depends on the type. All string keys (that are not indices) are in the order in which they were created. That means you have to create a new object and insert the properties in sorted order. – jabaa Apr 19 '21 at 08:17

2 Answers2

2

Your code is almost fine actually, the only missing piece is Object.fromEntries:

sorted = (obj).map(val =>
    Object.fromEntries(
        Object.entries(val).sort((a, b) => {
            const aa = a[0].split('/').reverse();
            const bb = b[0].split('/').reverse();
            return (aa > bb) - (aa < bb);
        })))
georg
  • 195,833
  • 46
  • 263
  • 351
1

You can't change the order of the keys in an object but you can convert the entries of an object to an array, sort the array and create a new object with sorted keys. Since ES6 the order the keys is specified.

const data = {
  '02/10/2021': 291692.14, '08/12/2023': 140908.90579999998, '09/04/2021': 14.4776, '09/06/2023': 863.8189, '10/06/2022': 388.9343999999999, '10/09/2021': 187.7662, '10/12/2021': -85216.4669
};


const res = Object
  .entries(data)
  .map(([date, value]) => ({ date, value }))
  .sort(({date: dateA}, {date: dateB}) => new Date(dateA) - new Date(dateB))
  .reduce((acc, el) => ({ ...acc, [el.date]: el.value }), {});

console.log(res)
jabaa
  • 2,998
  • 1
  • 3
  • 17