Re: On sorting

Sergiu Dumitriu <[email protected]> Sat, 25 May 2013 12:19:54 -0400
Newsgroups gmane.comp.jakarta.velocity.user
Message-ID <[email protected]>
On 05/25/2013 11:59 AM, [email protected] wrote:
>> ________________________________________
>> From: Sergiu Dumitriu [[email protected]]
>> Sent: 25 May 2013 17:49
>> To: Velocity Users List
>> Subject: Re: On sorting
>>
>> On 05/25/2013 11:39 AM, [email protected] wrote:
>>> I'm really getting mad! :-) Of course I know that the only reason for this is my lack of skills!!! Catching up...
>>>
>>> I do need some kind of security. Please, could you confirm this statement...
>>>
>>> Class SortTool can be used to sort any list of objects, but NOT a list of strings.
>>
>> Where did you get this from? It is not true, it can sort any list of
>> comparable things:
>>
>> #set ($strings = ['a', 'x', 'b', 'r', 'f'])
>> $sorttool.sort($strings) -> [a, b, f, r, x]
>>
> 
> Great! It is clear my wrong interpretation of this sentence...
> 
> "SortTool allows a user to sort a collection (or array, iterator, etc) on any arbitary set of properties exposed by the objects contained within the collection."
> 
> It is taken from the link below. No reference to strings, it speaks only about objects. But it is great to be wrong!

Yes, that is the more advanced part of the SortTool, the ability to sort
"beans" [1] by one or more of their properties. Unfortunately Strings
are not beans, so the sort methods that take a second parameter can't be
used for a simple list of strings.


[1] http://en.wikipedia.org/wiki/JavaBeans

> And, in this case, how could I get a reverse/desc order?

It's not possible using the SortTool, but in XWiki we have the
$collectionstool [2, 3] which does provide both a simple sort(List) and
a reverse(List) method, so you could do:

#set ($discard = $collectionstool.sort($list))
#set ($discard = $collectionstool.reverse($list))

Keep in mind that these two methods sort the list in place, and they
return "true" if the list could be sorted/reversed.


[2]
https://github.com/xwiki/xwiki-commons/blob/xwiki-commons-5.0/xwiki-commons-core/xwiki-commons-velocity/src/main/java/org/xwiki/velocity/tools/CollectionsTool.java

[3]
http://platform.xwiki.org/xwiki/bin/view/SRD/Navigation?api=collectionstool&xpage=embed

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