The Velocity SortTool allows you sort node elements in a multinode selection.
See Hannon Hill's Sort Tool knowledge base entry
The method requires these parameters:
Method addSortCriterion():
- selection string - An XPath expression specifying the node/node-set on which to sort.
- language - Specifies the two-letter ISO-639 language code to be used when sorting text data, defauls to "en".
- data type - Either "text", "number", or "qname", defaults to "text".
- sort order - Either "ascending" or "descending", defaults to "ascending".
- case order - Either "lower-first" or "upper-first", specifies whether upper-case or lower-case letters come first when sorting, defaults to "lower-first".
Then you issue a .sort method against the multinode element
<h2>My CD Collection</h2>
<table style="border: 3px solid #450084">
<tr style="background:#ccc999">
<th style="text-align:left">Title</th>
<th style="text-align:left">Artist</th>
</tr>
##LOOP
#set($cds = $_XPathTool.selectNodes($contentRoot, '//catalog/cd'))
$_SortTool.addSortCriterion("artist", "", "text", "ascending", "upper-first")
$_SortTool.sort($cds)
#foreach($cd in $cds)
#set ($artist = $cd.getChild('artist').value)
#set ($title = $cd.getChild('title').value)
<tr>
<td>$title</td>
<td>$artist</td>
</tr>
#end
##END LOOP
</table>
My CD Collection
Title | Artist |
---|---|
Romanza | Andrea Bocelli |
One night only | Bee Gees |
Empire Burlesque | Bob Dylan |
Hide your heart | Bonnie Tyler |
The very best of | Cat Stevens |
Greatest Hits | Dolly Parton |
Sylvias Mother | Dr.Hook |
Eros | Eros Ramazzotti |
Still got the blues | Gary Moore |
Unchain my heart | Joe Cocker |
Soulsville | Jorn Hoel |
For the good times | Kenny Rogers |
Midt om natten | Kim Larsen |
Pavarotti Gala Concert | Luciano Pavarotti |
1999 Grammy Nominees | Many |
The dock of the bay | Otis Redding |
When a man loves a woman | Percy Sledge |
Maggie May | Rod Stewart |
Stop | Sam Brown |
Black angel | Savage Rose |
Picture book | Simply Red |
Bridge of Spies | T`Pau |
Red | The Communards |
Private Dancer | Tina Turner |
Tupelo Honey | Van Morrison |
Big Willie style | Will Smith |