Elizbeth
Elizbeth

Reputation: 59

Using Ant Design and Moment Js, I'm still unable to figure out how can we sort columns using Dates?

I am using ant design and moments js for formation dates column in tables, but I'm still unable to figure out how can we sort columns using Dates. I'm doing something like:

sorter: (a, b) => {
      let a= !a.Created
        ? new Date().getTime()
        : new Date(a.Created).getTime();
      let b= !b.Created
        ? new Date().getTime()
        : new Date(b.Created).getTime();

      return [a, b].sort();
    }

But this isn't working. Created can be either "" or some date like "21 May, 2017" or "21 May, 2017 3:05 PM".

Upvotes: 2

Views: 1183

Answers (2)

Ibrahim
Ibrahim

Reputation: 336

Try this:

sorter: (a, b) => {
  if (moment(a.Created).isBefore(moment(b.Created))) {
     return -1;
  }
  return 1;
}

For more information about sorting in javascript, you can refer here.

Upvotes: 0

Rizwan
Rizwan

Reputation: 4463

You can try this with localeCompare(b.Created)

here is solution...

Working code:

sorter: (a, b) => { 
    return a.Created.localeCompare(b.Created)
  },

Upvotes: 2

Related Questions