Persian

Sort Persian strings alphabetically

Sorting in Javascript can be difficult to get right with non-Roman characters - for which special consideration must be made. This plug-in performs correct sorting on Persian characters.

Plug-in code

(function(){

var persianSort = [ 'آ', 'ا', 'ب', 'پ', 'ت', 'ث', 'ج', 'چ', 'ح', 'خ', 'د', 'ذ', 'ر', 'ز', 'ژ',
                    'س', 'ش', 'ص', 'ط', 'ظ', 'ع', 'غ', 'ف', 'ق', 'ک', 'گ', 'ل', 'م', 'ن', 'و', 'ه', 'ی', 'ي' ];

function GetUniCode(source) {
    source = $.trim(source);
    var result = '';
    var i, index;
    for (i = 0; i < source.length; i++) {
        //Check and fix IE indexOf bug
        if (!Array.indexOf) {
            index = jQuery.inArray(source.charAt(i), persianSort);
        }else{
            index = persianSort.indexOf(source.charAt(i));
        }
        if (index < 0) {
            index = source.charCodeAt(i);
        }
        if (index < 10) {
            index = '0' + index;
        }
        result += '00' + index;
    }
    return 'a' + result;
}

jQuery.extend( jQuery.fn.dataTableExt.oSort, {
    "pstring-pre": function ( a ) {
        return GetUniCode(a.toLowerCase());
    },

    "pstring-asc": function ( a, b ) {
        return ((a < b) ? -1 : ((a > b) ? 1 : 0));
    },

    "pstring-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: 'pstring', targets: 0 }
     ]
  } );