Skip to content

Instantly share code, notes, and snippets.

@Tomalak
Created July 6, 2012 17:11
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tomalak/3061389 to your computer and use it in GitHub Desktop.
Save Tomalak/3061389 to your computer and use it in GitHub Desktop.
A custom textExtraction function for jQuery tablesorter that allows better sorting for accented characters and dates in dd.mm.yyyy format. Primarily targeted at German text; might work well for other languages.
// Details: StackOverflow (http://stackoverflow.com/a/614397/18771)
// License: cc-by-sa (http://creativecommons.org/licenses/by-sa/3.0/)
// Extends: jQuery Tablesorter 2.0+ (http://tablesorter.com)
// -------------------------------------------------------------------
// file encoding must be UTF-8!
function getTextExtractor()
{
return (function() {
var patternLetters = /[öäüÖÄÜáàâéèêúùûóòôÁÀÂÉÈÊÚÙÛÓÒÔß]/g;
var patternDateDmy = /^(?:\D+)?(\d{1,2})\.(\d{1,2})\.(\d{2,4})$/;
var lookupLetters = {
"ä": "a", "ö": "o", "ü": "u",
"Ä": "A", "Ö": "O", "Ü": "U",
"á": "a", "à": "a", "â": "a",
"é": "e", "è": "e", "ê": "e",
"ú": "u", "ù": "u", "û": "u",
"ó": "o", "ò": "o", "ô": "o",
"Á": "A", "À": "A", "Â": "A",
"É": "E", "È": "E", "Ê": "E",
"Ú": "U", "Ù": "U", "Û": "U",
"Ó": "O", "Ò": "O", "Ô": "O",
"ß": "s"
};
var letterTranslator = function(match) {
return lookupLetters[match] || match;
}
return function(node) {
var text = $.trim($(node).text());
var date = text.match(patternDateDmy);
if (date)
return [date[3], date[2], date[1]].join("-");
else
return text.replace(patternLetters, letterTranslator);
}
})();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment