Author: hoebbel
Publication Date: 7/8/2010 9:23
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
Tags: Content, contentselect, fs4.2r2, knowledgebase, lambda, sort, template
Kommentare
5 Kommentare
Nice example how to use the template syntax :smileywink: (there a missing some hyphens).
Another possibility is to use the java class java.text.Collator:
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...
Jive did eat them :smileywink:. After reformatting the text, they are now visible [do not use the XML format for java/FirstSpirit Code]
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. :-)
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
Bitte melden Sie sich an, um einen Kommentar zu hinterlassen.