React-table: Sorting by date problem

Created on 11 Dec 2017  路  2Comments  路  Source: tannerlinsley/react-table

Hi, i have a column with dates in the format dd/mm/yyyy, the table must be default sorted by this column in desc way but im getting an error because it order by the highest day without consider the month.

Any idea how to solve it? I try with the custom sort example code but i get a weird outupt.

sortMethod: (a, b) => { if (a === b) { return 0; } const aReverse = a.split("").reverse().join(""); const bReverse = b.split("").reverse().join(""); return aReverse > bReverse ? 1 : -1; }

Most helpful comment

Without seeing the rest of the code it's also difficult to provide reliable advice. If your data is already a Date() object then you should ensure that you return that from your column accessor (this is because the accessor value is what is used to pass to routines like the sort and filter). Then you should "format" your date in a Cell handler. So that's the first piece to get right.

If your data is just text, then I suggest converting it to a Date (use something like Moment for reliability) in your accessor function (accessor: (row) => { }).

Bottom line - convert any data into the appropriate format in your accessor function so it can be used appropriately.

Then, you should be able to compare the dates in any way you like in your sortMethod.

All 2 comments

You can always convert the date format into timestamp new Date('mm/dd/yyy').getTime()

and then try sorting it.

sortMethod: (a, b) => {
a = new Date(a).getTime();
b = new Date(b).getTime();
   return b > a ? 1 : -1;
}

Without seeing the rest of the code it's also difficult to provide reliable advice. If your data is already a Date() object then you should ensure that you return that from your column accessor (this is because the accessor value is what is used to pass to routines like the sort and filter). Then you should "format" your date in a Cell handler. So that's the first piece to get right.

If your data is just text, then I suggest converting it to a Date (use something like Moment for reliability) in your accessor function (accessor: (row) => { }).

Bottom line - convert any data into the appropriate format in your accessor function so it can be used appropriately.

Then, you should be able to compare the dates in any way you like in your sortMethod.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hassankhan picture hassankhan  路  51Comments

Grsmto picture Grsmto  路  100Comments

Nizar-Rahme picture Nizar-Rahme  路  21Comments

BenMGilman picture BenMGilman  路  22Comments

vaidsu picture vaidsu  路  29Comments