Date sorting issue with "Ultimate date/time sorting..."
Date sorting issue with "Ultimate date/time sorting..."

Hi, I am using Razor to populate a table in which a column has a 'string date' returned such as "20 Dec 2019".
- I am using "$.fn.dataTable.moment('dd MMM yyyy')" to try to sort the column (ignores this).
- I have tried "DD MMM YYYY" and various other combinations.
- I have the moment.js and the datetime-moment.js declared in my _Layout.cshtml.
- I am using the .DataTable 'order' and 'columns' declarations to specify the default sort order and which columns can be sorted.
Am I correct in binding the table column to a date string? Is the columns restriction cancelling out the moment declaration?
Any other hints please?
This discussion has been closed.
Answers
You need to make sure your date format matches the formats listed here:
http://momentjs.com/docs/#/displaying/
dd
would match days like thisSu Mo ... Fr Sa
. There is noyyyy
format that I see.DD MMM YYYY
matches20 Dec 2019
and02 Dec 2019
but won't if you have2 Dec 2019
. In this case you would needD MMM YYYY
. All of the dates in the column need to match or it won't work. Hard to say without seeing an example with your data.This example does work with
DD MMM YYYY
:http://live.datatables.net/sebakahe/1/edit
Try changing
02 Dec 2019
to2 Dec 2019
and you will see it stops sorting correctly. Then change the format to$.fn.dataTable.moment('D MMM YYYY');
and the sorting works again.Kevin
Hi, I think the issue is if you have any non-date values in the column, it reverts to sorting alphabetically. I was trying to show an 'ended' contract as the word 'complete' in place of the next payment date. Obvious now I suppose but no script errors arose.
I was also sorting successfully by using 'data-order' on the containing element for example
You can use Orthogonal Data to display the data you want without affecting the data used for sorting.
Kevin
Thanks, that was useful.
...