sort a list ignoring capitals and german umlauts

hoebbel
Crownpeak employee
Crownpeak employee
3 5 2,011

Normally you can sort database entries within the function contentSelect. But you have to keep in mind, that the result is coming directly from the database. So if the database sorts case sensitive and the umlauts are sorted at the end of the list, you can either reconfigure the database or, if this is not wanted, you can sort the list afterwards.

Example:

<CMS_HEADER>

<CMS_FUNCTION name="contentSelect" resultname="list">
    <CMS_PARAM  name="schema" value="Testschema" />
    <QUERY entityType="SortMe">
        <ORDERCRITERIA attribute="text" descending="0" />
    </QUERY>
</CMS_FUNCTION>
</CMS_HEADER>

<h1>database sorted</h1>
$CMS_FOR(element,list)$
$CMS_VALUE(element.text)$<br>
$CMS_END_FOR$
<h1>manual sorted</h1>
$CMS_FOR(element,list.copy.sort(x -> x.text.toLowerCase().replaceAll("ö","oe").replaceAll("ä","ae")))$
$CMS_VALUE(element.text)$<br>
$CMS_END_FOR$

Output:

<h1>database sorted</h1>
Aachen<br>
Ende<br>
Zukunft<br>
anfang<br>
Ärger<br>
Österreich<br>
ähnlich<br>

<h1>manual sorted</h1>
Aachen<br>
ähnlich<br>
Ärger<br>
anfang<br>
Ende<br>
Österreich<br>
Zukunft<br>

Tested with FS4.2R2

5 Comments
Peter_Jodeleit
Crownpeak employee
Crownpeak employee

Nice example how to use the template syntax Smiley Wink (there a missing some hyphens).

Another possibility is to use the java class java.text.Collator:

   $CMS_SET(list, ["Aachen", "Ende", "Zukunft", "anfang", "Ärger", "Österreich", "ähnlich"])$
   $CMS_SET(comperator, class("java.text.Collator").instance(#global.locale))$
   $CMS_VALUE(list.copy.sort(comperator).toString("<br>\n"))$
Peter_Jodeleit
Crownpeak employee
Crownpeak employee

To make the usage simpler you could define a sorter e.g. in the global project settings template:

  $CMS_SET(comperator, class("java.text.Collator").instance(#global.locale))$
  $CMS_SET(sort, x -> x.copy.sort(comperator))$

Use it like this in all other templates:

   $CMS_VALUE(sort.eval(list).toString("<br/>\n"))$

This way you can define your own library of useful template functions...

hoebbel
Crownpeak employee
Crownpeak employee
(there a missing some hyphens)

Jive did eat them Smiley Wink. After reformatting the text, they are now visible [do not use the XML format for java/FirstSpirit Code]

sglock
I'm new here

Adding "alphabetical" and "alphabetisch" might be a good idea since searching the community did not deliver this blog entry when looking for these keywords. Now it does. 🙂

TimoMeister
Returning Responder

How to sort this list language dependent with #global.locale?

$CMS_SET(list, [{"city":"Bochum","id":323},{"city":"Aachen","id":123},{"city":"Ähhausen","id":274},{"city":"Ärgerdorf","id":242}])$

Solution:

https://community.e-spirit.com/message/23024#23024

Version history
Last update:
‎07-08-2010 02:23 AM
Updated by: