material-table icon indicating copy to clipboard operation
material-table copied to clipboard

Sort doesn't support Date or Datetime data types

Open martindeanlund opened this issue 7 years ago • 6 comments

Despite setting a column as type "date" or "datetime" the column sorting applies comparisons based on simple alphanumeric comparisons as if the data was just of type "String."

  1. ) Go to example column exhibit here: https://mbrn.github.io/material-table/#/docz-examples-02-example-column-types#column-types-example 2.) Edit code 3.) Swap years of sample data rows 4.) Click for ascending sort

Expected behavior Earliest year should display at the top of the sort, not the earliest month

martindeanlund avatar Dec 27 '18 22:12 martindeanlund

Hi @martindeanlund ,

You are right. I will resolve it asap.

mbrn avatar Dec 28 '18 20:12 mbrn

I think it might just be an issue with the example. I was able to sort correctly if the data provided is a proper JS Date and I use "dateTime" as the type for the column definitions, but I have to add Render arguments to convert back to a datestring for display purposes. I don't know if you want to support date strings as the data type and convert them to Date for sorting or support Date for the data type and convert to date Strings for rendering but either should work.

martindeanlund avatar Dec 28 '18 20:12 martindeanlund

I just look at example at docs now. I will look at code and test it with many cases. I may add support for string dates to be sorted properly. Thanks for giving detailed information

mbrn avatar Dec 28 '18 20:12 mbrn

No need improvement.

mbrn avatar Jan 07 '19 11:01 mbrn

A solution using Moment.js:

customSort = (a , b) => {
        return moment(moment(a[field], [formatDate]))
          .isBefore(moment(b[field], [formatDate])) 
            ? 1 
            : -1;
      };

formatDate: here you have to put what is the format of the string date (ex: 'DD-MM-YYYY - hh:mm A').

estefrare avatar Jan 15 '20 02:01 estefrare

Is this issue been solved? I got field with string date, the sort is still not working.

Screenshot 2021-05-12 164026

ytcitsupport-jlin avatar May 12 '21 23:05 ytcitsupport-jlin