1

I am using react table and wanted to sort date time but its not sorting properly either ascending order or descending order.

column defined code: Using below code to sort the date time.

{Header: 'Transaction Date',accessor: 'transationDate', style: {textAlign: "center"},
                          sortMethod: (a, b) => {
                            var a1 = new Date(a).getTime();
                            var b1 = new Date(b).getTime();
                          if(a1<b1)
                          return 1;
                          else if(a1<b1)
                          return -1;
                          else
                          return 0;
                          }
                        },

descending order Sorted Result:

enter image description here

1
  • 1
    Your if and else conditions are the same. Commented Mar 10, 2019 at 7:24

2 Answers 2

5

I don´t know if anyone still struggles with that but for me 'sortType' did the magic instead of 'sortMethod'.

{Header: 'Transaction Date',accessor: 'transationDate', style: {textAlign: "center"},
                      sortType: (a, b) => {
                        var a1 = new Date(a).getTime();
                        var b1 = new Date(b).getTime();
                      if(a1<b1)
                      return 1;
                      else if(a1>b1)
                      return -1;
                      else
                      return 0;
                      }
                    },
Sign up to request clarification or add additional context in comments.

Comments

3

This should work! Just flip the < with >.

{Header: 'Transaction Date',accessor: 'transationDate', style: {textAlign: "center"},
                          sortMethod: (a, b) => {
                            var a1 = new Date(a).getTime();
                            var b1 = new Date(b).getTime();
                          if(a1<b1)
                          return 1;
                          else if(a1>b1)
                          return -1;
                          else
                          return 0;
                          }
                        },

1 Comment

I tried but still its not sorting. Could you please suggest any thing else?

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.