velocity - getting distinct value

Hi there,

i am pulling through a list of items and the first letter of each item. I have all the items coming through with theior associated first letters but I want to then group these items but have no idea how to loop through the results to get each distinct letter.

Any possible solutions would be great - thanks

Hi Lisa

This should do it. You can use the same slot again to produce the actual list of items.

#initslot("yourSlotName" "")
              #if($sys.currentslot.relresults.size() > 0)
				
				<div class="atoz">
					<ul class="atozList">
					#set($letter = "")
                    #foreach( $relresult in $sys.currentslot.relresults )
        				#set($title = $relresult.node.getProperty("rx:displaytitle").String)
        				#set($initial = $title.substring(0,1))
							
						#if($letter != $initial)
        					<li><a href="#$initial">$initial</a></li>
        					#set($letter = $initial)
        				#end
        			 #end
        			</ul>
				</div>
#end
#endslot("yourSlotName")

You will have to change the HTML structure and class names to match your implementation.

Cheers
James

That’s exactly what i needed - thanks!