A jQuery Table Sorter Parser For German Vocabulary Lists

I still plan to go on vacation in April. I will be spending a week in Berlin. I am putting a lot more effort into learning German in order to prepare for my trip. I have been studying German for over a year, but not very intensely. I have created a custom help file for my notes on the German language. Many of the pages in this help file are HTML tables of vocabulary lists with the German and English words in columns. As I add words and rows to the list, the alphabetical order may be lost. Fortunately there is a plugin for jQuery which allows you to sort a HTML table in alphabetical order. I have been using the tablesorter plugin to sort my tables without a page refresh or server side code.

The only problem with that solution is the definite articles. I usually include the definite article before the nouns because you need to know the gender of a German noun. For example, Katze is a feminine noun so I place it in the Deutsch table column as die Katze so I will remember the noun’s gender. Unfortunately, that causes Katze to appear below every other masculine and neuter noun. A neuter noun requires the definite article das which comes before die when sorted in alphabetical order. So das Schwein (the pig) appears before die Katze (the cat) even though K words should be listed before S words. I could have solved this problem by placing the definite article after the word: Katze, die but that looks awkward. What I really wanted was to sort the column based on the second word.

Fortunately, you can create a custom parser for the tablesorter plugin which sorts the column based on the logic you provide in your code. I was able to write a custom parser that replaces the definite and indefinite articles in the table cell with empty strings so they do not affect the sort order. I can add to my logic to handle additional requirements like disregarding possessive pronouns when sorting the column,  mein Vater.

   1: // add parser through the tablesorter addParser method 
   2: $.tablesorter.addParser({ 
   3:     // set a unique id 
   4:     id: 'Deutsch', 
   5:     is: function(s) { 
   6:         // return false so this parser is not auto detected 
   7:         return false; 
   8:     }, 
   9:     format: function(s) { 
  10:         // format your data for normalization 
  11:         return s.toLowerCase().replace(/die /,'').replace(/der /,'').replace(/das /,''); 
  12:     }, 
  13:     // set type, either numeric or text 
  14:     type: 'text'
  15: }); 
  16:  
  17: $(function() { 
  18:     $("table").tablesorter({ 
  19:             sortList: [[0,0],[1,0]],  
  20:         headers: { 
  21:             0: { 
  22:                 sorter:'Deutsch'
  23:             }         
  24:         } 
  25:     }); 
  26: });   

This entry was posted in German, JavaScript and tagged , , , . Bookmark the permalink.

3 Responses to A jQuery Table Sorter Parser For German Vocabulary Lists

  1. Good work Robert. I have used tablesorter plugin in few of my uni projects and it worked fine as I never had a complicated issue like yours to sort. I must say you are a genius.

    [Reply]

  2. Manic says:

    Is there a possibility to override default text parser with custom parser? I have some local letters like “ČŠĆĐŽ” and sorting is not accurate. Letter “Š” comes after “Z” instead after S.

    [Reply]

    Robert S. Robbins Reply:

    I think that is a collating problem. I don’t know how to change JavaScript’s text sorting, but a database system would need the database collation changed.

    [Reply]

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>