Sort by column number when there is a letter after the number to sort by

Sort by column number when there is a letter after the number to sort by

felipecairellofelipecairello Posts: 4Questions: 2Answers: 0

Link to test case: https://bigwebuy.com/flora/colecciones - Click on "Buscar"
Debugger code (debug.datatables.net):
Error messages shown:
Description of problem: I'd like to sort the items in "N° Col" column by number, but the numbers have a suffix letter sometimes (separated by a space) and therefore it's sorting the items alphabetically. Ideally, it would sort it like this:
1
2
3
10 A
11 A
...

But currently it's sorting like this:
1
10 A
11 A
2
3
...

This question has accepted answers - jump to:

Answers

  • kthorngrenkthorngren Posts: 19,891Questions: 26Answers: 4,691
    Answer ✓

    Try the Natural Sorting plugin.

    Kevin

  • felipecairellofelipecairello Posts: 4Questions: 2Answers: 0

    Thanks a lot Kevin, this indeed solved the issue!

    I added this to the options when initializing the Datatable

    columnDefs: [
    { type: 'natural', targets: '_all' }
    ],

    Note the '_all' instead of 0 which is what the Datatables official documentation suggests.

    Many thanks lovely community !

  • kthorngrenkthorngren Posts: 19,891Questions: 26Answers: 4,691
    Answer ✓

    Using targets: '_all' may affect sorting of other columns. If so then set columnDefs.targets to target only those that need the natural sorting plugin.

    Kevin

Sign In or Register to comment.