1

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);
    });
  });
6
  • Your object keys (i.e.: 02/10/2021) are invalid strings. Did you forget to add "" quotes ? Also, it's impossible to sort objects. Commented Apr 19, 2021 at 8:08
  • yes I forgot them. Gonna edit Commented Apr 19, 2021 at 8:10
  • It is an object how can you sort in an object. for sorting there must be an array Commented Apr 19, 2021 at 8:10
  • It this data in dd/mm/yyyy or mm/dd/yyyy Commented Apr 19, 2021 at 8:14
  • @decpk It's dd/mm/yyyy. "08/12/2023" comes after "09/06/2023". Commented Apr 19, 2021 at 8:16

2 Answers 2

1

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);
        })))
Sign up to request clarification or add additional context in comments.

Comments

0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.