Hi Gary, your question is very application specific...
In your example you initialized the $newcollection with an instance of an ArrayList (Velocity' implementation of "[]"). This class has no notion of indexes - as you already noted... You could use a some type of Map to keep your index->record associations. Either you can use a tool to setup a map, or use the new Velocity 1.5 Map syntax, ToolboxManager, etc. Then you sort the result of map.keySet() and iterate over this: #foreach( $key in $SortTool.sort( $map.keySet() ) ) Entry: $map.get($key) #end If you used a java TreeMap to hold your key->entries, then you don't even need to post sort the keys. Hope this helps, :) Christoph 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]
