Sort by year-number

Sort by year-number

pippuccio76pippuccio76 Posts: 22Questions: 11Answers: 0

HI i have a table with a culumn formed as year-number .

For example 2024-55

The problem is when i have 2024-6 datatable sort 2024-6 before 2024-55 in desc mode

How can i solve ?

Answers

  • allanallan Posts: 64,010Questions: 1Answers: 10,554 Site admin

    The natural sorting plugin might do the job for you here.

    Allan

  • pippuccio76pippuccio76 Posts: 22Questions: 11Answers: 0

    Hi , i try to use natural sort :

    [CODE]
    $("#tabella-catalogo").DataTable({
    columnDefs: [{
    type: 'natural',
    target: 2
    }],
    order: [
    [2, 'desc'],
    ],
    keys: true,
    responsive: true,
    select: true,
    "language": {
    "sEmptyTable": "Nessun dato presente nella tabella",
    "sInfo": "Vista da START a END di TOTAL Record",
    "sInfoEmpty": "Vista da 0 a 0 di 0 Record",
    "sInfoFiltered": "(filtrati da MAX Record totali)",
    "sInfoPostFix": "",
    "sInfoThousands": ".",
    "sLengthMenu": "Mostra MENU Record",
    "sLoadingRecords": "Caricamento...",
    "sProcessing": "Elaborazione...",
    "sSearch": "Cerca:",
    "sZeroRecords": "Nessun risultato.",
    "oPaginate": {
    "sFirst": "Inizio",
    "sPrevious": "Precedente",
    "sNext": "Successivo",
    "sLast": "Fine"
    },
    "paginate": {
    "previous": "<i class='fas fa-arrow-left'></i>",
    "next": "<i class='fas fa-arrow-right'></i>"
    }
    },
    "drawCallback": function() {
    $(".dataTables_paginate > .pagination").addClass("pagination-rounded");
    }
    });

    [/CODE]

    but doesn't work :

  • kthorngrenkthorngren Posts: 21,670Questions: 26Answers: 5,017

    Did you load the plugin code?

    I built a simple test case with a sample of your data and it seems to work:
    https://live.datatables.net/wuhemamo/1/edit

    If you still need help then please update my test case to show the issue or provide a link to your page so we can take a look.

    Kevin

  • pippuccio76pippuccio76 Posts: 22Questions: 11Answers: 0
    edited February 14

    This is my source view :

    <script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/2.2.2/js/jquery.dataTables.js"></script>
    <script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/plug-ins/2.2.2/sorting/natural.js"></script>
    
    <script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.8.4/moment.min.js"></script>
    <script src="//cdn.datatables.net/plug-ins/1.10.13/sorting/datetime-moment.js"></script>
    
    <!-- Datatables -->
    
    <script>
        $(document).ready(function() {
    
    
            
    
    
    
    
    
            $.fn.dataTable.moment('DD-MM-YYYY HH:mm:ss');
    
            $("#tabella-catalogo").DataTable({
                columnDefs: [{
                    type: 'natural',
                    target: 2
                }],
                order: [
                    [2, 'desc'],
                ],
                keys: true,
                responsive: true,
                select: true,
                "language": {
                    "sEmptyTable": "Nessun dato presente nella tabella",
                    "sInfo": "Vista da _START_ a _END_ di _TOTAL_ Record",
                    "sInfoEmpty": "Vista da 0 a 0 di 0 Record",
                    "sInfoFiltered": "(filtrati da _MAX_ Record totali)",
                    "sInfoPostFix": "",
                    "sInfoThousands": ".",
                    "sLengthMenu": "Mostra _MENU_ Record",
                    "sLoadingRecords": "Caricamento...",
                    "sProcessing": "Elaborazione...",
                    "sSearch": "Cerca:",
                    "sZeroRecords": "Nessun risultato.",
                    "oPaginate": {
                        "sFirst": "Inizio",
                        "sPrevious": "Precedente",
                        "sNext": "Successivo",
                        "sLast": "Fine"
                    },
                    "paginate": {
                        "previous": "<i class='fas fa-arrow-left'></i>",
                        "next": "<i class='fas fa-arrow-right'></i>"
                    }
                },
                "drawCallback": function() {
                    $(".dataTables_paginate > .pagination").addClass("pagination-rounded");
                }
            });
    
            
    
        });
    </script>
    

    But doesn't work properly

    Edited by Kevin: Syntax highlighting. Details on how to highlight code using markdown can be found in this guide

  • kthorngrenkthorngren Posts: 21,670Questions: 26Answers: 5,017

    The code snippet is not enough to help us debug. The problem is likely due to something in your data. Can you update my test case to show the issue or provide a link to your page so we can see why the plugin doesn't work with your data.

    Kevin

  • kthorngrenkthorngren Posts: 21,670Questions: 26Answers: 5,017

    Does that column have HTML wrappaing the numeric data? If so maybe you need to use natural-nohtml instead. See th eplugin docs for details.

    Kevin

Sign In or Register to comment.