4

I am displaying below 3 fields userid,value & upadtetime in which I want to sort by UPDATE.TIME in desc order. Please suggest how to do this.

<c:forEach var="comment" items="${document['kcmeta/comment']}">

<g2:out value="${mm:MasterValue('datamodel_userInfo',comment.USER_ID)}"/>

<c:out value="${comment.VALUE}" />

<span class="searhResultLightGrayText"><c:out value="${comment.UPDATE_DATE}" /></span>          
</c:forEach>
codeMan
  • 5,730
  • 3
  • 27
  • 51
Tanu Garg
  • 3,007
  • 4
  • 21
  • 29

1 Answers1

0

There is no way that you can sort any kind of collections by using the c:forEach JSTL tag, However you can handle this with the usage of Scriplets and embed JAVA code into your JSP.

<%
    Collections.sort(yourList, new Comparator<Document>() {
        public int compare (Document d1, Document d2) {
            return d1.getUpdateTime().compare(d2.getUpdateTime());
        }
    });
%>

and then you'll be able to iterate over your sorted list:

<c:forEach var="comment" items="<%=youList%>">
    ...
</c:forEach>
Hazem Farahat
  • 3,790
  • 2
  • 26
  • 42
Eder
  • 1,874
  • 17
  • 34
  • This is not the best way to do this. See the discussion here: https://stackoverflow.com/questions/6638550/how-can-i-sort-a-list-several-different-ways-in-a-jsp – wi2ard Jul 10 '17 at 14:11
  • @GabiM: Agree, It is not the best way to handle it, however I just exposed another way, not necessarily the best one to follow. – Eder Jul 11 '17 at 04:09
  • Sure, it does the job. I just commented for others to know there are better alternatives – wi2ard Jul 11 '17 at 12:00