ok Confluence 5.5, User Macros & Velocity: Array List sortieren | CMS - Webdesign - Website

Confluence 5.5, User Macros & Velocity: Array List sortieren

Das Sortieren von Array Lists ist in Velocity z.B. so zu lösen:

## neue Liste anlegen
#set( $array = ["a", "B", "d", "C", "f", "e"] )
###### sort
###
#set($size=$array.size())
#foreach($junk in $array) ##Bubble sort takes n^2 passes
 #set($actual=-1)##Having trouble with math on $velocityCount -- keeping my own count
  #foreach($line in $array)
   #set($actual=$actual+1)
   #if($velocityCount<$size) ##Preventing bad array access
    ##### Compare this and previous
    ##### If this is smaller, swap
    #if ($line.compareToIgnoreCase($array.get($velocityCount)) > 0 )
      #set ($tmp=$array.get($velocityCount))
      #set ($junk=$array.set($velocityCount,$line))
      #set ($junk=$array.set($actual,$tmp))
    #end
   #end
  #end
#end
#foreach( $item in  $array )
  <li>$item
#end

Anwendungsfall:

Z.B. die User einer Gruppe nach dem FullName und nicht nach dem Username sortieren.

## UserMacro sortiert die User einer Gruppe nach dem FullName
#set($group = $userAccessor.getGroup("confluence-administrators") )
## Liste aller User einer Gruppe
#set($groupUsers = $userAccessor.getMemberNamesAsList($group) )
## neue Liste anlegen...
#set( $array = [] )
## ...und mit den FullNames aus den Usern in der User Gruppe befüllen
#foreach( $username in  $groupUsers )
  #set( $fullName = $array.add( $userAccessor.getUserByName($username).getFullName() ) )
#end
###### sortieren (Bubble Sortierung)
###
#set($size=$array.size())
#foreach($junk in $array) ##Bubble sort takes n^2 passes
 #set($actual=-1)##Having trouble with math on $velocityCount -- keeping my own count
  #foreach($line in $array)
   #set($actual=$actual+1)
   #if($velocityCount<$size) ##Preventing bad array access
    ##### Compare this and previous
    ##### If this is smaller, swap
    #if ($line.compareToIgnoreCase($array.get($velocityCount)) > 0 )
      #set ($tmp=$array.get($velocityCount))
      #set ($junk=$array.set($velocityCount,$line))
      #set ($junk=$array.set($actual,$tmp))
    #end
   #end
  #end
#end
#foreach( $item in  $array )
  <li>$item
#end