Sorting a list based on base property

MrFjords's Avatar

MrFjords

18 Jun, 2015 06:56 PM

I'm having trouble sorting a list of numbers in descending order

I have a list:

$list = [1434816000000, 1427385600000, 1427126400000, 1428336000000, 1429545600000, 1430755200000, 1431964800000]

And I want to sort it so I used:

#set ( $sortedChildren = $_SortTool.sort($list) )

But I can't figure out how to set it in descending order. I tried the following and it didn't work.

#set ( $sortedChildren = $_SortTool.sort($list, ":desc") )
  1. 1 Posted by Ryan Griffith on 18 Jun, 2015 08:04 PM

    Ryan Griffith's Avatar

    Hi,

    The Sort Tool will only accept multiple arguments if you are attempting to sort a map, but not an array.

    One issue you will run into is that Velocity will generate an ArrayList when using the [1,2,3] notation, so you need to convert it to a native Array before passing it into the Sort Tool. This still won't allow you to specify the direction; however, so you basically need to sort and then reverse the array using a loop. Consider the following:

    #set ( $list = [1,5,4,2,7] )
    
    ## Sort the list in ascending order
    #set ( $list = $_SortTool.sort($list.toArray()) )
    
    ## Reverse the list by looping from size-1 to 0
    #foreach ( $i in [$_MathTool.sub($list.size(),1)..0] )
        $list.get($i)
    #end
    

    Please let me know if you have any questions.

    Thanks!

  2. Tim closed this discussion on 30 Jun, 2015 06:45 PM.

Discussions are closed to public comments.
If you need help with Cascade CMS please start a new discussion.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac

 

26 Aug, 2016 01:19 PM
25 Aug, 2016 03:02 PM
25 Aug, 2016 12:50 PM
24 Aug, 2016 08:43 PM
24 Aug, 2016 07:20 PM
21 Aug, 2016 01:20 PM