Sorting with International Characters

Sorting with International Characters

atthelimitsatthelimits Posts: 3Questions: 1Answers: 1

I have a list within a datatable which works well, right up to the point of sorting international characters.

ABC
BCD
XYZ
ØPQ

How can I get it to sort like:

ABC
BCD
ØPQ
XYZ

Thanks,

Marty

This question has accepted answers - jump to:

Answers

  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin
    Answer ✓

    Hi Marty,

    Have a look at this blog post which should help.

    Allan

  • atthelimitsatthelimits Posts: 3Questions: 1Answers: 1

    Thanks Alan, but what if I don't know the locale?

    Basically, I have a list of people who are from around the world. Is there a general sort method that will sort 99% of the foreign characters correctly without specifying a locale for the whole table? (Unless it's a general locale)

    Thanks,

    Marty

  • atthelimitsatthelimits Posts: 3Questions: 1Answers: 1
    edited February 2018 Answer ✓

    Wow, I should play first, then post.

    Worked right out the box (almost).

    For everyone else, here's what sorted it correctly.

    Thanks.

    var collator = new window.Intl.Collator( 'gb' );
    var types = $.fn.dataTable.ext.type;
    
    delete types.order['string-pre'];
    types.order['string-asc'] = collator.compare;
    types.order['string-desc'] = function ( a, b ) {
        return collator.compare( a, b ) * -1;
    };
      
    $('#myTable').DataTable({
        paging: false,
        "columnDefs": [ { "type": "string", "targets": 2 } ],
        order: [[ 2, "asc" ]]
    });
    
This discussion has been closed.