Sorting issues (1-10-12-2-20 etc)

Sorting issues (1-10-12-2-20 etc)

EmilBr78EmilBr78 Posts: 6Questions: 2Answers: 0

Heya!

I use the most basic table setup:

$(document).ready(function(){
$('#datatable').DataTable()
} );

...and I'm having issues with it not sorting properly. The numerical fields are lexicographically sorted, probably due to whitespace (if I've googled correctly). How do I fix it?

Here's the table in action:
https://www.cyberzoo.se/ciklider-artregister-lista.html

Best regards
Emil

This question has an accepted answers - jump to answer

Answers

  • EmilBr78EmilBr78 Posts: 6Questions: 2Answers: 0

    FWIW, adding the examples found here:
    https://datatables.net/reference/option/columns.type

    $(document).ready(function(){
    $('#datatable').DataTable( {
    "columnDefs": [
        { "type": "html", "targets": 0 }
      ]
    } );
    

    ...breaks the script.

  • allanallan Posts: 48,097Questions: 1Answers: 6,917 Site admin

    I'm currently getting an error:

    SyntaxError: Unexpected end of input

    when loading the page.

    However, I can see the plain unformatted table. If we take the Storlek (cm) column as an example, it contains numbers such as:

    12
    14-20
    6,5
    4.8
    

    The first one obviously is fine. The second, you would need a custom plug-in to sort. The third and fourth, I'm not sure if they are both meant to be decimal characters? If so, you need to pick one and be consistent with it.

    Allan

  • EmilBr78EmilBr78 Posts: 6Questions: 2Answers: 0

    Hi Allan! Sorry for the late reply! The issues you pointed out have been corrected, the table is essentially the way we want it now, we've added a min/max search that works like a charm. But it still doesn't sort the columns the way we want and I can't see what's wrong.

    If you add "30" to the min search box and "120" to the max box, and then sort the "Volym"-column you'll see what's wrong.

  • allanallan Posts: 48,097Questions: 1Answers: 6,917 Site admin
    Answer ✓

    A number of the cells in that column have   which is non-numeric, hence the issue. If you put - or an empty string in, it will work okay.

    Allan

  • EmilBr78EmilBr78 Posts: 6Questions: 2Answers: 0
    • did the trick, THANK YOU!

    Best regards
    Emil

Sign In or Register to comment.