Search the FirstSpirit Knowledge Base
Hello,
I want to sort the values (_list.value.name) behind the selected checkboxes but don't know how. Has anyone an idea? Thanks
FORM
<CMS_INPUT_CHECKBOX name="cs_location" gridWidth="3" hFill="yes" sortOrder="ascending" useLanguages="no">
<CMS_INCLUDE_OPTIONS type="database">
<LABELS>
<LABEL lang="*">#item.name</LABEL>
</LABELS>
<TABLE>FSStoinside.news_locations</TABLE>
</CMS_INCLUDE_OPTIONS>
<LANGINFOS>
<LANGINFO lang="*" label="Show ONLY news of following location/department (optional)"/>
<LANGINFO lang="DE" label="NUR Nachrichten für folgenden Standort oder Abteilung anzeigen (optional)"/>
</LANGINFOS>
</CMS_INPUT_CHECKBOX>
HTML
$CMS_FOR(_list,cs_location)$
<li>$CMS_VALUE(_list.value.name)$</li>
$CMS_END_FOR$
Does not work!
$CMS_FOR(_list,cs_location.sort(x->x.value.name))$
cs_location (value type of CheckboxEditorValue) is a set and "sort" isn't supported for this datatype. You have to transform it to a list first. E.g. like this: [] + cs_location (i.e. adding all elements of the set to an empty list). The result than could be sorted: ([] + cs_location).sort(..)
tl;dr
Sort is only supported for lists.
Do you get any error? I suspect the list cs_location isn't modifyable. So doing a copy first could do the trick: cs_location.copy.sort(x->x.value.name)
No, that doesn't work either ...
But the unsorted for-loop from your initial post is working? Please check your log and report back.
Unsorted loop works.
The Log
ERROR ($CMS_FOR( _list, cs_location.copy.sort(x -> x.value.name) )$ at 27, 1): Cannot iterate over: class de.espirit.firstspirit.parser.eval.Undefined
inside of: Template 'Standard' (id=544431)
inside of: $CMS_VALUE(#global.page.body("column_right"))$ - at 30, 7
inside of: Template 'local news' (id=757642)
inside of: $CMS_TRIM(level:3)$ - at 19, 1
inside of: $CMS_FOR( _list, cs_location.copy.sort(x -> x.value.name) )$ - at 27, 1
cs_location (value type of CheckboxEditorValue) is a set and "sort" isn't supported for this datatype. You have to transform it to a list first. E.g. like this: [] + cs_location (i.e. adding all elements of the set to an empty list). The result than could be sorted: ([] + cs_location).sort(..)
tl;dr
Sort is only supported for lists.
That works out! Thank you very much!
$CMS_SET(_locationsSet,[]+cs_location)$
$CMS_FOR(_list,_locationsSet.sort(x->x.value.name))$