1

I've UNIX timestamps in a response and I'm sorting them as:

var sortedData = data.sort(function (x, y) {
    return x.created - y.created
})

Here are some of the values of created

1612840160306
1612717863299
1612715819573
1612853308834
1612842118101
1612767742861
...

Then I'm passing the sortedData to a state which then re-renders as:

                        {userSortedData.map((data, index) =>
                            <MyTestCell
                                key={index}
                                name={data.personName}
                                phone={data.phone}
                                createdOn={data.created}
                            />
                        )}

In my view, I can see that the first cell is assumed

enter image description here

And a cell below it is

enter image description here

Whereas it should be that the cell displaying Feb 7 date should be above Feb 6 but it's not.

2
  • 2
    whatever you did is working perfectly fine, but in ascending order thus you are seeing in 6th before 7th. Try descending order data.sort((x, y) => y.created - x.created). Commented Apr 15, 2021 at 5:10
  • @Ravikumar thank you for pointing it out, It worked now. If you post this as an answer I'll accept that. Commented Apr 15, 2021 at 5:13

1 Answer 1

2

Whatever you did is working perfectly fine, but in ascending order thus you are seeing in 6th before 7th. Try descending order

data.sort((x, y) => y.created - x.created)
Sign up to request clarification or add additional context in comments.

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.