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?
Community moderators have prevented the ability to post new answers.
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
Jonathan,
I am also struggling with sorting of arraylists. How can I sort this list alphabetically?
#set( $myArrayList = ["a", "B", "d", "C", "f", "e"] )
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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()}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Don't see how I would create a collection in velocity on that page
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Sorry, I misread that, thinking you were working in a Velocity template, not a user macro. My bad!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I just decided that I will do the sorting with javascript after the user macro is rendered.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Community moderators have prevented the ability to post new answers.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.