[email protected] #2682
Replies: 4 comments 3 replies
-
It's working for me, I am also getting string but in an ISO format In the frontend I also use the date pipe by angular to transform the date to the medium format, but yeah the sorting on date is working out of the box. |
Beta Was this translation helpful? Give feedback.
-
Interesting my dates are just simple MM/DD/YYYY. I tried the data pipe but that didn't work either. The only way I could get it to sort correctly was implementing a custom sort routine but I don't like how it's implemented. customSort(event: SortEvent) {
} |
Beta Was this translation helpful? Give feedback.
-
@4gDad Hi. This is what happens internally: https://github.com/primefaces/primeng/blob/4d3337c6574b0ae48fdac7587c7c1d9f5a537eab/src/app/components/table/table.ts#L1601 For dates stored as strings (like '2020-08-02'), localeCompare() works as long as the string format is consistent (i.e., YYYY-MM-DD format). This is because in this format, the string comparison is naturally chronological: '2020' < '2021' (because "2" comes before "3" in string comparison). So, as long as dates are formatted in ISO 8601 (YYYY-MM-DD), localeCompare() will sort them correctly, because the string comparison will naturally order them by year first, then by month, then by day. Demo: If you have a different date format, map the string to a Date object and use the mapped values. This is generally a better solution because you might run into problems when using timezones and other more complex values. |
Beta Was this translation helpful? Give feedback.
-
I have a p-table that contain dates, the sorting does not seem to work. I'm guessing because the dates are strings. What is the work around for this issue. I am using PrimeNg 17
Beta Was this translation helpful? Give feedback.
All reactions