Locale Sort

Locale Sort

golyogolyo Posts: 3Questions: 0Answers: 0
edited February 2016 in Free community support

I'd like to use localeCompare function, when sorting string, or html types.
http://www.w3schools.com/jsref/jsref_localecompare.asp

The language can be choose on site, and we set it on session. So localeCompare should work well.

We have to hack sort to each language (like hungarian, polish, ...), because I can't find the way, to use localecompare.

My hack:

var translate_re = /[a,á,e,é,i,í,o,ó,ö,ő,ü,ű]/g;

var translate = {
    "a":"aa","á":"ab","e":"ea","é":"eb","i":"ia","í":"ib","o":"oa","ó":"ob","ö":"oc","ő": "od","u":"ua","ú":"ub","ü":"uc","ű" :"ud"
};

var hun_accute = function (d) {
    return d.replace(translate_re, function(match) {
            return translate[match];
        });
};

var _orig_html_pre = $.fn.dataTable.ext.type.order['html-pre'];
$.fn.dataTable.ext.type.order['html-pre'] = function ( d ) {
    return hun_accute(_orig_html_pre(d));
};
var _orig_string_pre = $.fn.dataTable.ext.type.order['string-pre'];
$.fn.dataTable.ext.type.order['string-pre'] = function ( d ) {
    var r = hun_accute(_orig_string_pre(d));
    console.log(r);
    return r;
};

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

Replies

  • allanallan Posts: 61,985Questions: 1Answers: 10,162 Site admin

    There is a plug-in which uses localeCompare, and I even included it in DataTables core at one point, but the performance was poor and the results sometimes completely wrong!

    There is a new i18n API in Javascript which I'm going to write a sorting plug-in for soon (WebKit are in the middle of adding support for it, and Firefox already has it. Not sure about Chrome).

    Allan

  • golyogolyo Posts: 3Questions: 0Answers: 0

    Thx for quick answer.

    The type can be html, string, numeric, special enums... as I see.
    The language is an other concept.

    I can handle special columns, but want to use advantages of auto detect api.
    Think use localCompare (or not) should be in settings, because its the same usages for all languages.

    Golyo

  • golyogolyo Posts: 3Questions: 0Answers: 0

    Maybe
    jQuery.fn.dataTableExt.localeCompare should be use as default, can be override, with fastest, optimalized plugin.

This discussion has been closed.