I have two string variables in a Velocity template. I want to do a lexicographic compare. I tried this:
#if ($string1 > $string2)
#set ($largest = $string1)
#else
#set ($largest = $string2)
#end
## (assume `$string1` and `$string2` are never Null)
So, if $string1
contains the string "dog"
and $string2
contains the string "cat"
, then $largest
will end up containing the string "dog"
.
The above code generates this message:
Left side of '>=' operation is not a Number
How can I do the compare I want?
You can use Java String's compareTo method to compare strings
The result is a positive integer if this String object lexicographically follows the argument string
#if ($string1.compareTo($string2) > 0)
#set ($largest = $string1)
#else
#set ($largest = $string2)
#end