Java JSTL foreach - 使用特定字段对列表进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19176283/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JSTL foreach - sort list with particular field
提问by Tanu Garg
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.
我在下面 3 个字段中显示用户 ID、值和更新时间,我想在其中按 UPDATE.TIME 降序排序。请建议如何做到这一点。
<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>
回答by Eder
There is no way that you can sort any kind of collections by using the c:forEach
JSTLtag, However you can handle this with the usage of Scripletsand embed JAVA code into your JSP.
您无法使用c:forEach
JSTL标记对任何类型的集合进行排序,但是您可以使用Scriplets来处理这个问题,并将 JAVA 代码嵌入到您的 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>