Sort List of Objects in Velocity

Davin Studer
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 1, 2013

Is there any way to sort a list of Velocity objects? Specifically I have a user macro that has a list of page attachments using $content.getLatestVersionsOfAttachments(). I want to sort that by title or last modification date depending upon what theuser selects in the macro settings. Has Atlassian exposed any helper methods for sorting lists?

4 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

6 votes
Answer accepted
Jonathan Simonoff
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 10, 2013

I wrote code to sort a list in a user macro. I ended up using a bubble sort because it is (I think) a fixed maximum number of operations -- all of the sorting algorithms I found (including this one, bubble sort) were written using until or while loops, which don't exist in Velicity.

###### 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

Stefan Baader August 11, 2014

Jonathan,

I am also struggling with sorting of arraylists. How can I sort this list alphabetically?

#set( $myArrayList = ["a", "B", "d", "C", "f", "e"] )

Stefan Baader August 11, 2014

Thanks Jonathan,

for small arra lists this works fine. I tried this sorting the confluence-users by their fullName and that was a kind of denial of service attack ;-)

But small groups: OK

Sam Hasler August 15, 2016

The above code doesn't appear to work in the latest release of confluence $array.size() appears to be invalid VTL in a set statement, even though it will output correctly as ${array.size()}

0 votes
Wolfgang Lutz April 26, 2016

Why you didn't use this?

$myArray.sort(null)
0 votes
Matthew J. Horn
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 2, 2013

You could load your list into a Collection and sort the collection. This is touched on in the Velocity docs:

http://velocity.apache.org/engine/devel/developer-guide.html#supportforiterativeobjectsforforeach

Davin Studer
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 3, 2013

Don't see how I would create a collection in velocity on that page

Matthew J. Horn
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 3, 2013

Sorry, I misread that, thinking you were working in a Velocity template, not a user macro. My bad!

0 votes
Davin Studer
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 2, 2013

I just decided that I will do the sorting with javascript after the user macro is rendered.

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

TAGS
AUG Leaders

Atlassian Community Events