kskaradzinski
kskaradzinski

Reputation: 5084

jquery tablesorter plugin sort just one way

Hi have wrote tablesorter plugin to sort Polish chars but plugin sorts just one way http://jsfiddle.net/Gk43v/ here is the example and plugin code

$.tablesorter.addParser({
   id: 'polish-chars' ,
   type: 'text',
   is: function(s)
   {
       return false;
   },
   format: function(s)
   {
       return
       s.replace('\u0105'/g, 'a')
       .replace('\u0104'/g, 'A')
       .replace('\u0118'/g, 'E')
       .replace('\u0119'/g, 'e')
       .replace('\u0107'/g, 'c')
       .replace('\u0106'/g, 'C')
       .replace('\u0143'/g, 'N')
       .replace('\u0144'/g, 'n')
       .replace('Ó'/g, 'O')
       .replace('ó'/g, 'o')
       .replace('\u0141'/g, 'L')
       .replace('\u0142'/g, 'l')
       .replace('\u015a'/g, 'S')
       .replace('\u015b'/g, 's')
       .replace('\u0179'/g, 'Z')
       .replace('\u017a'/g, 'z')
       .replace('\u017b'/g, 'Z')
       .replace('\u017c'/g, 'z')
   }
});

Edit: When using this plugin with tablesorter it didn't show an error /g, so it should look like

$.tablesorter.addParser({
   id: 'polish-chars',
   type: 'text',
   is: function(s)
   {
       return false;
   },
   format: function(s)
   {
       return s.replace('ą', 'a')
               .replace('Ą', 'A')
               .replace('Ę', 'E')
               .replace('ę', 'e')
               .replace('ć', 'c')
               .replace('Ć', 'C')
               .replace('Ń', 'N')
               .replace('ń', 'n')
               .replace('Ó', 'O')
               .replace('ó', 'o')
               .replace('Ł', 'L')
               .replace('ł', 'l')
               .replace('Ś', 'S')
               .replace('ś', 's')
               .replace('Ź', 'Z')
               .replace('ź', 'z')
               .replace('Ż', 'Z')
               .replace('ż', 'z');
   }
});

and work just like I wanted to work.

Upvotes: 2

Views: 836

Answers (1)

Mottie
Mottie

Reputation: 86473

When using replace with /g, don't use quotes (updated demo):

$.tablesorter.addParser({
    id: 'polish-chars',
    type: 'text',
    is: function(s) {
        return false;
    },
    format: function(s) {
       return s.replace(/\u0105/g, 'a')
       .replace(/\u0104/g, 'A')
       .replace(/\u0118/g, 'E')
       .replace(/\u0119/g, 'e')
       .replace(/\u0107/g, 'c')
       .replace(/\u0106/g, 'C')
       .replace(/\u0143/g, 'N')
       .replace(/\u0144/g, 'n')
       .replace(/Ó/g, 'O')
       .replace(/ó/g, 'o')
       .replace(/\u0141/g, 'L')
       .replace(/\u0142/g, 'l')
       .replace(/\u015a/g, 'S')
       .replace(/\u015b/g, 's')
       .replace(/\u0179/g, 'Z')
       .replace(/\u017a/g, 'z')
       .replace(/\u017b/g, 'Z')
       .replace(/\u017c/g, 'z');
    }
});

Alternatively, you could try out my fork of tablesorter which does character equivalents replacements when the sortLocaleCompare option is true. Just add the missing Polish characters to the list:

$.extend( $.tablesorter.characterEquivalents, {
    "a" : "\u0105", // ą
    "A" : "\u0104", // Ą
    "c" : "\u0107", // ć
    "C" : "\u0106", // Ć
    "e" : "\u0119", // ę
    "E" : "\u0118", // Ę
    "l" : "\u0142", // ł
    "L" : "\u0141", // Ł
    "n" : "\u0144", // ń
    "N" : "\u0143", // Ń
    "o" : "\u00f3", // ó
    "O" : "\u00d3", // Ó
    "s" : "\u015b", // ś
    "S" : "\u015a", // Ś
    "z" : "\u017a\u017c", // źż
    "Z" : "\u0179\u017b" // ŹŻ
});

Upvotes: 2

Related Questions