DataTables Persian Number Sorting

Sorts columns containing UTF-8 Persian numbers

Sorts a column containing Persian numbers. Persian numbers can easily be mapped 1:1 to latin numbers - ۱ = 1, ۲ = 2, ۱۲ = 12 and so on.

Plug-in code

jQuery.extend(jQuery.fn.dataTableExt.oSort, {
    "kh-persian-numbers-pre": function (a) {
        function toEnglishNumber(strNum) {
            var pn = ["۰", "۱", "۲", "۳", "۴", "۵", "۶", "۷", "۸", "۹"];
            var en = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"];
            var cache = strNum;

            for (var i = 0; i < 10; i++) {
                var regex_fa = new RegExp(pn[i], 'g');
                cache = cache.replace(regex_fa, en[i]);
            }

            return cache;
        }

        return parseFloat(toEnglishNumber(a))
    },
    "kh-persian-numbers-asc": function (a, b) {
        return ((a < b) ? -1 : ((a > b) ? 1 : 0))
    },
    "kh-persian-numbers-desc": function (a, b) {
        return ((a < b) ? 1 : ((a > b) ? -1 : 0))
    }
});

CDN

This plug-in is available on the DataTables CDN:

JS

Note that if you are using multiple plug-ins, it is beneficial in terms of performance to combine the plug-ins into a single file and host it on your own server, rather than making multiple requests to the DataTables CDN.

Version control

If you have any ideas for how this plug-in can be improved, or spot anything that is in error, it is available on GitHub and pull requests are very welcome!

Example

$('#example').DataTable( {
     columnDefs: [
       { type: 'kh-persian-numbers', targets: 0 }
     ]
  } );