Hi Gary,

Velocity uses internally an ArrayList for the $newcollection
in your example. This does not have any indexing capability.

You would need something like a Map. You can create one with
a tool, directly with the new Velocity 1.5 Map feature, or
with the ToolboxManager of the velotiy-tools subproject.

Populate it with ($index should be an Integer):
   #call( $map.put( $index, $rec ) )

And access the sorted entries:
#foreach ($key in $SortTool.sort($map.getKeys()) )
  Entry: $map.get($key)
#end


You could avoid post sorting the keys using a TreeMap:
#foreach ( $key in $treeMap.getKeys() )
  Entry: $map.get($key)
#end


Hope this helped,
:) Christoph

P.S. my code example uses a global macro:
  #macro( call $ref )#if( $ref )#end#end

Gary M. Catlin wrote:
> Hello all,
> 
> I have data stored in a collection. There is no field within the
> collection that is suitable for use as a sorting parameter. Externally,
> I can generate this sorting index, but I need a means for adding this
> index into my collection.
> 
> I know how to scan through records, obtain the correct ones, and insert
> them into a new collection. I also know how I can generate the value of
> the required index. However, I don't know how to associate the index
> with the appropriate record in the new collection.
> 
> Below is my current code for scanning, choosing the valid records and
> including them into the new collection. Any help on how to include the
> index value would be appreciated
> 
> #set ($newcollection = [])
> #foreach ($rec in $data.collection)
>        #if($rec.name == "valid")                     ## choose on valid
> records
>            #set ($index = ???)                       ## calculate index
> value
>            #set ($n = $newcollection.add($rec))      ## add record and
> index to new collection
>       #end
> #end
> 
> Then, I would like to be able to retrieve the records as follows:
> 
> #foreach ($newrec in $SortTool.sort($newcollection, "index")   ## choose
> records from new collection, sorted by "index"
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to