Re: In VTL, simply reverse a list with java.util.Collection static method

Sergiu Dumitriu <[email protected]>
Newsgroups gmane.comp.jakarta.velocity.user
Message-ID <[email protected]>
On 05/06/2013 03:15 PM, Travis P wrote:
> Via web searching, I've found quite a few people reversing lists by writing
> out loops.
> 
> Seems to me, we should be able to something like:
> 
> #set ($lst = ["a","b","c"])
> #set ($revlst = $lst)
> $Collection.reverse($revlst)
> 
> where $Collection.reverse refers to the java.util.Collections static method
> static void*reverse<http://w3.hursley.ibm.com/java/docs/jdk5.0/api/java/util/Collections.html#reverse(java.util.List)>
> *(List <http://w3.hursley.ibm.com/java/docs/jdk5.0/api/java/util/List.html>
> <?> list)
>           Reverses the order of the elements in the specified list.
> 
> but I don't see any way to do that without writing a custom tool.  Which
> seems heavy-handed for such a basic thing.  Am I missing something?
> 
> (I'm using velocity-1.7, velocity-tools-view-2.0, running on tomcat-7.0.39)
> 
> -Travis
> 

You should use the existing SortTool tool.

http://velocity.apache.org/tools/devel/javadoc/org/apache/velocity/tools/generic/SortTool.html

For security reasons, Velocity doesn't allow accessing raw classes or
instantiating new objects. You must always start from a valid variable
placed in the context.

See http://velocity.apache.org/tools/releases/2.0/ for more details
about tools and how to configure them.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.