Search code examples
jspjstljsp-tags

Is it possible to sort in a JSP?


Is there any way to sort alphabetically two strings variables in a JSP, for example by using jstl? Or at least determinate which string would be first alphabetically?

I have been trying with a code like this one. It seems to work with some simple examples, but I'm not sure that it is considering alphabetical order. Can anyone confirm whether "gt" operator takes alphabetical order when the variable is not a number?

<c:set value="abc" var="var1"/>
<c:set value="def" var="var2"/>
<c:if test="${var2 gt var1}">
    <p>var1 is first</p>
</c:if>
<c:if test="${var1 gt var2}">
    <p>var2 is first</p>
</c:if>

Thanks


Solution

  • Regarding the part of your question about doing the EL operators on Strings, the example at http://java.sun.com/products/jsp/syntax/2.0/syntaxref207.html and some practical tests confirm that gt takes alphabetical precedence on Strings.

    In your example def > abc will be true, since d > a