title: Sort a table with UTF-8 encoded values alphabetically
body: <p>I am storing dictionary entries in a Lua table, using it as an array. I want to sort the entries from Lua, so that I can add new ones without having to move to the correct position myself (which gets quite tedious soon). However, I am facing several problems:</p>
  
  <ol>
  <li>Many words contain non-ASCII characters, which makes the built-in comparison operator for strings unsuitable for the task (for instance, it makes <em>amputar</em> come before <em>ámbito</em>).</li>
  <li>There are words from various languages (all Western, though), namely Spanish, German and English. The problem here is that, probably, different languages have different notions of the alphabetical order. Since the main language is Spanish, I would like to use its rules, although I'm unsure as to whether that will work with characters not contained in the Spanish alphabet.</li>
  <li>Some words contain capital letters, or, even worse, start with them. For example, all German nouns start with upper-case letters. By the built-in comparison operator, the capital letters come before their lower-case siblings, which is not my desired behaviour; I would like upper-case letters to be treated exactly as their lower-case counterparts.</li>
  </ol>
  
  <p>Take, for example, the following table:</p>
  
  <pre><code>local entries =
  {
      'amputar',
      'Volksgeist',
      'ámbito'
  }
  </code></pre>
  
  <p>Those entries should be ordered like this:</p>
  
  <blockquote>
    <p>ámbito<br>
    amputar<br>
    Volksgeist</p>
  </blockquote>
  
  <p>However, with my current code, the output is wrong:</p>
  
  <pre><code>local function compare_utf8_strings( o1 , o2 )
      -- Using the built-in non-UTF-8-aware non-locale-aware string comparison operator
      return o1 &lt; o2
  end
  
  table.sort( entries , function ( a , b ) return compare_utf8_strings( a , b ) end )
  
  for i, entry in ipairs(entries) do
      print( entry )
  end
  </code></pre>
  
  <p>That outputs:</p>
  
  <blockquote>
    <p>Volksgeist<br>
    amputar<br>
    ámbito</p>
  </blockquote>
  
  <p>Could you please take the following code, and hack it to fulfill my requirements?</p>
  
  <pre><code>local entries =
  {
      'amputar',
      'Volksgeist',
      'ámbito'
  }
  
  local function compare_utf8_strings( o1 , o2 )
      -- Hack here, please, accomplishing my requirements
  end
  
  table.sort( entries , function ( a , b ) return compare_utf8_strings( a , b ) end )
  
  for i, entry in ipairs(entries) do
      print( entry )
  end
  </code></pre>
  
  <p>It should output this:</p>
  
  <blockquote>
    <p>ámbito<br>
    amputar<br>
    Volksgeist</p>
  </blockquote>
  
  <p>As an additional requirement, this Lua code is all inside LuaTeX, which currently supports 5.2 version of the language. As for external libraries, I guess it's possible to use them.</p>
  
  <p>I am a novice in the Lua camp, so, please, forgive any error I have made, and feel free to notify it, so I fix it.</p>