Date sorting with moment does not work

Date sorting with moment does not work

LukasLLukasL Posts: 26Questions: 10Answers: 0
edited January 2022 in Free community support

Link to test case:
http://jsfiddle.net/kwe217qu/

Debugger code (debug.datatables.net):

Error messages shown:
None

Description of problem:
I'm getting data through AJAX and trying to fix formatted date sorting for a column. I looked around the web and I feel like I'm doing everything right with the moment.js stuff, but for some reason it continues to sort the formatted dates incorrectly as if they're just strings.

How do I get the dates to sort correctly when they're in ddd, MMM Do h:mm A format (ex: Sat, Jan 8th 3:23 PM)?

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 20,267Questions: 26Answers: 4,764
    Answer ✓

    Thats an interesting problem. I think I just learned something new about Datatables. Looks like, when the column type is string Datatables sets the orthogonal data for the sort operation to lower case so that the case doesn't affect sorting. In your test case the the sort string is set to lower case, ie, wed, dec 29th 8:37 am so it doesn't match the format you set.

    I added a second column that uses orthogonal data to set the display and filter operations to the format you specify but leaves the sort with the original data.
    http://jsfiddle.net/Ltyn86q1/

    You will see this in the console output:

    column 0 type: string
    column 1 type: date
    Sort Data:
    0: "wed, dec 29th 8:37 am"
    1: 1640785032570

    And you will see that the second column sorts properly.

    Kevin

  • LukasLLukasL Posts: 26Questions: 10Answers: 0

    Kevin, your help is invaluable. Thank you very much! My table is sorting properly now and we learned something new.

Sign In or Register to comment.