Extensions Wiki » Extensions » Sort a list of XWiki objects

Sort a list of XWiki objects

Last modified by Vincent Massol on 2012/11/12 16:28
cogSorts a list of XWiki objects by a given object property name
Developed by

Eduard Moraru

LicenseGNU Lesser General Public License 2.1

Table of contents


The problem with directly using $sorttool.sort(...) on a list of XWiki objects is that $sorttool expects the objects to have getters for the properties you want to sort on. However, XWiki objects don`t work that way and, instead, you have to do $object.getProperty(propertyname).value to get the actual property value.

To work around this problem, you can use the velocity macro below which supports sorting by a property name and allows multiple values for the sorted property.


{{velocity output='false'}}
 * Sort a list of XWiki objects using an property as comparator.
 * @param inputObjects the list of XWiki objects to sort.
 * @param propertyName the name of the XWiki object property to sort the inputObjects by. Multiple values for the same property are allowed.
 * @param outputObjects the list where to append the sorted objects. This list must not be null and should be empty before calling the macro.
#macro (sortXWikiObjectsList $inputObjects $propertyName $outputObjects)
 #set ($objectsMap = {})
 #foreach ($object in $inputObjects)
   #set ($key = $object.getProperty($propertyName).value)
   #set ($sameKeyObjects = $objectsMap.get($key))
   #if (!$sameKeyObjects)
     #set ($sameKeyObjects = [])
     #set ($discard = $objectsMap.put($key, $sameKeyObjects))
   #set ($discard = $sameKeyObjects.add($object))
 #foreach ($key in $sorttool.sort($objectsMap.keySet()))
   #set ($sameKeyObjects = $objectsMap.get($key))
   #foreach ($sameKeyObject in $sameKeyObjects)
     #set ($discard = $outputObjects.add($sameKeyObject))


Use case: Sort comment object in the page using the 'comment' property

#set ($objects = $doc.getObjects('XWiki.XWikiComments'))
#set ($sortedObjects = [])
#sortXWikiObjectsList($objects, 'comment', $sortedObjects)

## Print the result
#foreach ($object in $sortedObjects)

Output: (assuming the comments contain each a single number in an scrambled order)


Created by Eduard Moraru on 2012/11/12 16:07

Download XWiki

My Recent Modifications

Get Connected