Sorting Output in Velocity
Hi all,
Looking for some advice. I'm delving deeper into Velocity as I continue my development learning, and I have a question about sorting. I've spent quite a bit of time on this trying to figure it out myself as I find it the best way to learn, but I'm not having too much luck. Apologies if the answer is obvious.
I've written a script to populate part of an email based on the funds that a user is subscribed to. I've included an example of what this looks like below, while substituting the data for mock data. Basically the script uses #foreach to loop through a list of funds that the user is subscribed to receive information about (the info is stored on our own in-house CMS and pulled from there - $lead.fundcode). It outputs HTML which contains links to a fund page and factsheets. There can be as little as 1 fund output, or as many as 22 for select users. It depends on how many they're subscribed to. $countrylink dictates which instance of our website the user is directed to.
Due to issues with Velocity and the tracking of multiple links I had to use Sandy's brilliant workaround using #evaluate and the script was working fine.
However, I've been asked to find a way of sorting the order in which funds are output. For example, we want Asian funds to appear together, European funds to appear together etc. At the moment this doesn't happen and they're output alphabetically.
How do I go about doing this? I've tried to use the sorter function but the below is clearly incorrect as nothing is pulling through.
#set( $allFundLinks = {
"Fund 1" : {
"name": "Test Fund 1",
"link" : "test-fund-1-link",
"image" : "Fund1%20Imagery.jpg",
"order" : "1"
},
"Fund 2" : {
"name": "Test Fund 2",
"link" : "test-fund-2-link",
"image" : "Fund2%20Imagery.jpg",
"order" : "2"
},
"Fund 3" : {
"name": "Test Fund 3",
"link" : "test-fund-3-link",
"image" : "Fund3%20Imagery.jpg",
"order" : "3"
}
} )
#if ($lead.countryCode.equals("NL") )
#set ( $countryLink ="netherlands" )
#elseif ($lead.countryCode.equals("BE") )
#set ($countryLink ="belgium" )
#elseif ($lead.countryCode.equals("LU") )
#set ($countryLink = "luxembourg" )
#end
#foreach( $key in $lead.fundcode.split(";") )
#set( $fundLink = $sorter.sort($allFundLinks,"order")[$key] )
#evaluate( "${esc.h}set( ${esc.d}fundLink_${foreach.count} = ${esc.d}fundLink )" )
#end
#if( $fundLink_1 )
*/HTML Output*/
#end
#if( $fundLink_2 )
*/HTML Output*/
#end
#if( $fundLink_3 )
*/HTML Output*/
#end