Language

Rob Garrison edited this page Dec 13, 2017 · 11 revisions

Wiki: Home | FAQ | Customize | Snippets | Search | Language | Changes | Older-changes | Change summary

Languages: Czech | 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!

Czech

  • We'll need to switch the sort text code from sugar.js' sort to sort the Czech alphabet in specific order:

    AaÁáÄäBbCcČčDdĎďEeÉéĚěFfGgHh-CHCh-IiÍíJjKkLlMmNnŇňOoÓóÖöPpQqRrŘřSsŠšTtŤťUuÚúŮůÜüVvWwXxYyÝýZzŽž
  • But Sugarjs doesn't allow defining multiple characters in the sort order variable like "CH" which is in the Czech alphabet.

  • So we need to add a special textExtraction function to replace the CH with a single character placeholder. In this example, we will replace "CH" with "Æ" and "ch" with "æ" (in case you need a case sensitive sort).

  • See this Stackoverflow thread & this demo for more details.

    $(function () {
      Array.AlphanumericSortOrder = 'AaÁáÄäBbCcČčDdĎďEeÉéĚěFfGgHhÆæIiÍíJjKkLlMmNnŇňOoÓóÖöPpQqRrŘřSsŠšTtŤťUuÚúŮůÜüVvWwXxYyÝýZzŽž';
      Array.AlphanumericSortIgnoreCase = true;
      // see https://github.com/andrewplummer/Sugar/issues/382#issuecomment-41526957
      Array.AlphanumericSortEquivalents = {};
    
      // replace "Ch" and "ch" with a placeholder... it can be anything
      // in this example, I'm replacing ch with "æ" and Ch or CH with "Æ"
      // these characters have been added to the Array.AlphanumericSortOrder
      // between "h" and "I" - according to http://en.wikipedia.org/wiki/Czech_orthography
      var replaceCH = function( node ) {
        return $(node).text()
        .replace(/(Ch|CH)/g, '\u00c6')
        .replace(/ch/g, '\u00e6');
      };
    
      // in this example, the second and fourth columns (zero-based index used below) need the special
      // Czech language sort - http://jsfiddle.net/Mottie/Gk43v/23/
      $("table").tablesorter({
        theme: 'blue',
        // table = table object; get config options from table.config
        // column is the column index (zero-based)
        ignoreCase: false,
        textExtraction : {
          1: replaceCH,
          3: replaceCH
        },
        textSorter: {
          1 : Array.AlphanumericSort, // alphanumeric sort from sugar (http://sugarjs.com/arrays#sorting)
          3 : Array.AlphanumericSort
        }
      });
    });

Icelandic

  • We'll need to switch the sort text code from sugar.js' sort to sort the Icelandic alphabet in specific order: AaÁáBbCcDdÐðEeÉéĘęFfGgHhIiÍíJjKkLlMmNnOoÓóPpQqRrSsTtUuÚúVvWwXxYyÝýZzÞþÆæÖö.

  • The Sugar library has been modified to make sorting much easier, so all the extra coding in the original example (preserved below) is no longer needed; just make sure to include the sugar libray ;)

    $(function () {
    
        Array.AlphanumericSortOrder = 'AaÁáBbCcDdÐðEeÉéĘęFfGgHhIiÍíJjKkLlMmNnOoÓóPpQqRrSsTtUuÚúVvWwXxYyÝýZzÞþÆæÖö';
        Array.AlphanumericSortIgnoreCase = true;
        // see https://github.com/andrewplummer/Sugar/issues/382#issuecomment-41526957
        Array.AlphanumericSortEquivalents = {};
    
        $('table.tablesorter').tablesorter({
            widgets: ['zebra'],
            textSorter: {
                0 : Array.AlphanumericSort
            }
        });
    
    });
  • The code below is the original example left here for prosperity. It required copying specific code functions from Sugarjs to get the sort to work. The library has since exposed those sort functions so it greatly simplifies the code (see the example above)

  • Here is the original 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
      });
    });

Polish

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" // ŹŻ
});