Wiki source code of Macros
Last modified by SuperNico Laub on 2025/09/18 17:55
Hide last authors
| author | version | line-number | content |
|---|---|---|---|
| |
1.1 | 1 | {{velocity output='false'}} |
| 2 | #* | ||
| 3 | * Sort a list of XWiki objects using a property as comparator. | ||
| 4 | * | ||
| 5 | * @param inputObjects the list of XWiki objects to sort. | ||
| 6 | * @param propertyName the name of the XWiki object property to sort the inputObjects by. Multiple values for the same | ||
| 7 | * property are allowed. | ||
| 8 | * @param outputObjects the list where to append the sorted objects. This list must not be null and should be empty | ||
| 9 | * before calling the macro. | ||
| 10 | *# | ||
| 11 | #macro (sortXWikiObjectsList $inputObjects $propertyName $outputObjects) | ||
| 12 | #set ($objectsMap = {}) | ||
| 13 | #foreach ($object in $inputObjects) | ||
| 14 | #set ($key = $object.getValue($propertyName)) | ||
| 15 | #set ($sameKeyObjects = $objectsMap.get($key)) | ||
| 16 | #if (!$sameKeyObjects) | ||
| 17 | #set ($sameKeyObjects = []) | ||
| 18 | #set ($discard = $objectsMap.put($key, $sameKeyObjects)) | ||
| 19 | #end | ||
| 20 | #set ($discard = $sameKeyObjects.add($object)) | ||
| 21 | #end | ||
| |
2.1 | 22 | #foreach ($key in $collectiontool.sort($objectsMap.keySet())) |
| |
1.1 | 23 | #set ($sameKeyObjects = $objectsMap.get($key)) |
| 24 | #foreach ($sameKeyObject in $sameKeyObjects) | ||
| 25 | #set ($discard = $outputObjects.add($sameKeyObject)) | ||
| 26 | #end | ||
| 27 | #end | ||
| 28 | #end | ||
| 29 | {{/velocity}} |