Date Sorting for MM/DD/YYYY Failing

Date Sorting for MM/DD/YYYY Failing

jtomneyjtomney Posts: 2Questions: 1Answers: 0

My page has a table that should initialize with the record create date in descending fashion. This was implemented the last quarter of 2017 but today I see that 01/02/2018 is being sorted as older than the December 2017 dates (attached screenshot). It is an Intranet site so I cannot offer a link to it.

I am using the plug in that should enable the sort [line 6 below in the code snippet])

                $.ajax(
                    {
                        data: { startDt: $('#startDt').val(), endDt: $('#endDt').val() },
                        datatype: 'json',
                        success: function (rosterData) {
                            $.fn.dataTable.moment('MM/DD/YYYY','en-US');
                            $('#tblRoster').DataTable({
                                data: JSON.parse(rosterData.firstChild.textContent),

The column that is being sorted has the following configuration:

                                columns: [
                                    {
                                        'data': 'CreatedDt' ,
                                        "type": "date ",
                                        "render": function (value) {
                                            if (value === null) return "";
                                            return window.moment(value).format('MM/DD/YYYY');
                                        }
                                    },

A snippet of the JSON being used as data for the table is also attached. Thanks for any insight as to why it seems to be sorting more in string than date fashion.

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,341Questions: 26Answers: 4,954
    edited January 2018 Answer ✓

    Built a test case for you using your data and config snippets:
    http://live.datatables.net/laximahi/1/edit

    Found that you have an extra space here:
    "type": "date ",

    Remove the space and it should sort correctly, like the test case.

    Also, it looks like you are using Bootstrap but also including extra Datatables CSS files. You have duplicate sorting arrows; one Bootstrap and one Datatables. For Bootstrap 3 you should include dataTables.bootstrap.min.css but not jquery.dataTables.min.css. More details here:
    https://datatables.net/manual/styling/bootstrap

    Kevin

  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin

    If you are using the Moment plug-in for DataTables you shouldn't actually specify the columns.type option at all. The plug-in adds automatic type detection for the format that was specified.

    Allan

  • jtomneyjtomney Posts: 2Questions: 1Answers: 0

    Thank you folks - removing the "type" attribute from the column configuration or correcting the space in it both resulted in resolution of the date sort issue. Cheers!

This discussion has been closed.