Velocity中两个字符串的字典序比较

Lexicographic compare of two strings in Velocity

我在 Velocity 模板中有两个字符串变量。我想做一个词典比较。我试过这个:

#if ($string1 > $string2)
  #set ($largest = $string1)
#else
  #set ($largest = $string2)
#end
## (assume `$string1` and `$string2` are never Null)

所以,如果 $string1 包含字符串 "dog" 并且 $string2 包含字符串 "cat",那么 $largest 最终将包含字符串 "dog".

以上代码生成此消息:

Left side of '>=' operation is not a Number

如何进行我想要的比较?

你可以使用Java字符串的compareTo方法来比较字符串

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