Home > Net >  problem while sorting the date form to recent to oldest in react native
problem while sorting the date form to recent to oldest in react native

Time:09-20

I'm working on a react native project and I'm really new to react-native.

In my project, I'm retrieving the attached object from one of the APIs of the system. For my frontend (react-native) project, I need to sort that array according to the start date, from the latest start date to the oldest.

I have attached my code below, but this is not working properly. I found a few solutions on the internet, but non of them worked for me.

    Object.keys(this.state.events).map((key) => this.state.events[key])
               .sort((a, b) => b.startDate - a.startDate);

This is return from the API,

{
  "2010": [
    {
      "id": 251,
      "eventName": "asasasas",
      "startDate": "2010-01-01",
      "endDate": "2017-01-01",
      "description": null
    }
  ],
  "2022": [
    {
      "id": 120,
      "eventName": "Hello 123",
      "startDate": "2022-09-01",
      "endDate": "2022-09-06",
      "description": "Nothing"
    },
    {
      "id": 176,
      "eventName": "Hello 1234567",
      "startDate": "2022-09-13",
      "endDate": "2022-10-13",
      "description": "Nothing"
    },
    {
      "id": 331,
      "eventName": "Hello 1",
      "startDate": "2022-09-20",
      "endDate": "2022-11-01",
      "description": "asasasasasasas\nasasasasasasas\nasasasasasasasasas\nasasasasasasasasas\nasasasasasasasasaas\nasasasasasasasasasasas\nasasasasasasasasasasasas\nasasasasasasasasasasasasas\nasasasasasasassasasasasasas\nasasasasasasasasasasasasasas\nasasasasasasasasasasasasasasas\nasasasasasasasasasasasasasasasasa\nasasasasasasasa"
    }
  ]
}

below should be the result after sorting,

{
      "2010": [
        {
          "id": 251,
          "eventName": "asasasas",
          "startDate": "2010-01-01",
          "endDate": "2017-01-01",
          "description": null
        }
      ],
      "2022": [
        {
          "id": 331,
          "eventName": "Hello 1",
          "startDate": "2022-09-20",
          "endDate": "2022-11-01",
          "description": "asasasasasasas\nasasasasasasas\nasasasasasasasasas\nasasasasasasasasas\nasasasasasasasasaas\nasasasasasasasasasasas\nasasasasasasasasasasasas\nasasasasasasasasasasasasas\nasasasasasasassasasasasasas\nasasasasasasasasasasasasasas\nasasasasasasasasasasasasasasas\nasasasasasasasasasasasasasasasasa\nasasasasasasasa"
        },
        {
          "id": 176,
          "eventName": "Hello 1234567",
          "startDate": "2022-09-13",
          "endDate": "2022-10-13",
          "description": "Nothing"
        },
        {
          "id": 120,
          "eventName": "Hello 123",
          "startDate": "2022-09-01",
          "endDate": "2022-09-06",
          "description": "Nothing"
        }
      ]
    }

can someone please help me figure out what I'm missing?

Thank you in advance.

CodePudding user response:

With this date-format you can sort the startDates as strings (DESC). Your atempt, a-b doesn't work, as they are no numbers, but you don't need to go through the effort to parse them either.

Here the complete update

this.setState(state => {
  const events = {};
  const sortFn = (a, b) => b.startDate.localeCompare(a.startDate);

  for (const [year, items] of Object.fromEntries(state.events)) {
    events[year] = items.slice().sort(sortFn)
  }

  return { events }
})
  • Related