Skip to content
Mottie edited this page Feb 20, 2013 · 12 revisions

Languages: Icelandic | Polish

Language Character Equivalents

When sorting languages, accented or other special characters may not sort properly within the table. So by using the sortLocaleCompare option the equivalent character can be assigned and thus correct the sorting issue. See the main document demo on how to apply the code provided below - essentially, add this code before initializing the table.

Please help contribute information to this page!

Note: If you would like to contribute but you are having trouble figuring out the unicode value (e.g. \u0105) for a character, the easiest solution I've found would be to use Google's closure compiler.

  • Go to the Google closure compiler site

  • Hit the reset link

  • Enter the following, replacing the characters as needed:

      a = "ą"
    

    You will end up with a result of

      a="\u0105";
    
  • Copy and paste the code into the object

Thanks!

  • We'll need to switch the sort text code from sugar.js' sort to sort the Icelandic alphabet in specific order: AÁBCDÐEÉĘFGHIÍJKLMNOÓPQRSTUÚVWXYÝZÞÆÖ.

  • Here is a working demo.

  • See issue #212 for more details.

    $(function(){
    
      /* sortBy functions & helpers extracted and slightly modified from
      * Sugar Library v1.3.7
      * Freely distributable and licensed under the MIT-style license.
      * Copyright (c) 2012 Andrew Plummer
      * http://sugarjs.com/arrays#sorting
      */
      var array = {
        AlphanumericSortIgnoreCase  : true,
        AlphanumericSortEquivalents : {}
      },
    
      // order = 'AÁÀÂÃĄBCĆČÇDĎÐEÉÈĚÊËĘFGĞHıIÍÌİÎÏJKLŁMNŃŇÑOÓÒÔPQRŘSŚŠŞTŤUÚÙŮÛÜVWXYÝZŹŻŽÞÆŒØÕÅÄÖ',
      // equiv = 'AÁÀÂÃ,CÇ,EÉÈÊË,IÍÌİÎÏ,OÓÒÔ,Sß,UÚÙÛÜ',
      // modified order to match Icelandic sorting - see https://github.com/Mottie/tablesorter/issues/212
      order = 'AÁBCDÐEÉĘFGHIÍJKLMNOÓPQRSTUÚVWXYÝZÞÆÖ',
      equiv = '',
    
      sortBy = function(a,b){
        return typeof a === "string" && typeof b === "string" ? collateStrings(a,b) : a < b ? -1 : a > b ? 1 : 0;
      },
    
      // Alphanumeric collation helpers
      collateStrings = function(a, b) {
        var aValue, bValue, aChar, bChar, aEquiv, bEquiv, index = 0, tiebreaker = 0;
        a = getCollationReadyString(a);
        b = getCollationReadyString(b);
        do {
          aChar  = getCollationCharacter(a, index);
          bChar  = getCollationCharacter(b, index);
          aValue = getCollationValue(aChar);
          bValue = getCollationValue(bChar);
          if (aValue === -1 || bValue === -1) {
            aValue = a.charCodeAt(index) || null;
            bValue = b.charCodeAt(index) || null;
          }
          aEquiv = aChar !== a.charAt(index);
          bEquiv = bChar !== b.charAt(index);
          if (aEquiv !== bEquiv && tiebreaker === 0) {
            tiebreaker = aEquiv - bEquiv;
          }
          index += 1;
        } while (aValue != null && bValue != null && aValue === bValue);
        if (aValue === bValue) return tiebreaker;
        return aValue < bValue ? -1 : 1;
      },
    
      getCollationReadyString = function(str) {
        if (array.AlphanumericSortIgnoreCase) {
          str = str.toLowerCase();
        }
        return str.replace(array.AlphanumericSortIgnore, '');
      },
    
      getCollationCharacter = function(str, index) {
        var chr = str.charAt(index), eq = array.AlphanumericSortEquivalents || {};
        return eq[chr] || chr;
      },
    
      getCollationValue = function(chr){
        var order = array.AlphanumericSortOrder;
        return chr ? order.indexOf(chr) : null;
      },
    
      equivalents = {};
    
      array.AlphanumericSortOrder = $.map(order.split(''), function(str) {
        return str + str.toLowerCase();
      }).join('');
    
      $.each(equiv.split(','), function(i,set) {
        var equivalent = set.charAt(0);
        $.each(set.slice(1).split(''), function(i,chr) {
          equivalents[chr] = equivalent;
          equivalents[chr.toLowerCase()] = equivalent.toLowerCase();
        });
      });
    
      $('table.tablesorter').tablesorter({
        widgets    : ['zebra'],
        textSorter : sortBy
      });
    });

From this StackOverflow question, so thanks to skowron-line for sharing this data.

$.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" // ŹŻ
});
Clone this wiki locally