-1

I'm using DataTable in my Django application. Everything works fine, but sorting by date is wrong. It's sorting as string, not date.

html:

<script>
        $(document).ready(function () {
            $('.document-table').DataTable({
                "order": [
                    [0, "desc"]
                ],
                "bInfo": false,
                "pagingType": "full_numbers",
            });
        });
</script>
Bob Reynolds
  • 929
  • 3
  • 8
  • 21
  • There are various [questions and answers](https://stackoverflow.com/search?q=[datatables]+date+sort) relating to date sorting in DataTables. Using the `moment.js` library is one popular approach. See [here](https://datatables.net/blog/2014-12-18). – andrewJames Sep 03 '20 at 13:26
  • Here is one specific example using `moment`: [Datatable: date / time sorting plug-in not ordering](https://stackoverflow.com/questions/44226347/datatable-date-time-sorting-plug-in-not-ordering) – andrewJames Sep 03 '20 at 13:27
  • Another great approach is to use [orthogonal data](https://datatables.net/manual/data/orthogonal-data). This allows you to have multiple versions of a value - one for display purposes, one for sorting, one for filtering. So you can display the date as `25-Dec-2020` but behind the scenes you can use `20201225` as the sort value. – andrewJames Sep 03 '20 at 13:29
  • Does this answer your question? [Datatable date sorting dd/mm/yyyy issue](https://stackoverflow.com/questions/12003222/datatable-date-sorting-dd-mm-yyyy-issue) – Dani Sep 07 '20 at 11:00

1 Answers1

0

I've solved this problem just by removing the line "order": [[0, "desc"]], and ordering dates in django's views.py side.

Bob Reynolds
  • 929
  • 3
  • 8
  • 21