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

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

KCSKCS Posts: 10Questions: 2Answers: 0

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?

Answers

  • kthorngrenkthorngren Posts: 20,145Questions: 26Answers: 4,736
    edited May 2019

    You need to make sure your date format matches the formats listed here:
    http://momentjs.com/docs/#/displaying/

    dd would match days like this Su Mo ... Fr Sa. There is no yyyy format that I see. DD MMM YYYY matches 20 Dec 2019 and 02 Dec 2019 but won't if you have 2 Dec 2019. In this case you would need D 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 to 2 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

  • KCSKCS Posts: 10Questions: 2Answers: 0

    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

    <td data-order='@item.DateFinal.ToString("yyyy-MM-dd-HH:mm:ss")'>
    
  • kthorngrenkthorngren Posts: 20,145Questions: 26Answers: 4,736

    You can use Orthogonal Data to display the data you want without affecting the data used for sorting.

    Kevin

  • KCSKCS Posts: 10Questions: 2Answers: 0

    Thanks, that was useful.

  • exon02exon02 Posts: 3Questions: 1Answers: 0
    edited February 2020

    ...

This discussion has been closed.