Solved

How to iterate over a collection backwards using c:foreach in JSTL

Posted on 2008-09-29
7
4,446 Views
Last Modified: 2013-11-24
I have an array of strings that I need to list out backwards. For example:
List...
AAAA
BBBB
CCCC
nnnn

I'd like to c:out this as...
nnnn
BBBB
CCCC
AAAA

Where nnnn is the last string in the array.

This is an array (collection) so I'll never know the end - meaning it can't be a fixed start and end. My thinking was to get the array length and use that value minus 1. Is this possible? If so, what is the variable name of the actual length of an array in JSTL so I know where to subtract from?

Here is my current forEach...

<c:forEach var="trailCat" items="${thisCategory.crumbTrail}" varStatus="status">

That will display
AAAA
BBBB
CCCC
nnnn
with no problem.

Thanks,
biglarrrr
0
Comment
Question by:biglarrrr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
7 Comments
 
LVL 27

Expert Comment

by:rrz
ID: 22601308
Try this.  
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%
  String[] arr = {"AAAA","BBBB","CCCC","DDDD","nnnn"};
  request.setAttribute("myArray", arr);
%>


             ${myArray[arrayLength - status.count]}
0
 
LVL 27

Expert Comment

by:rrz
ID: 22601335
If you want each element on a separate line, then add    inside loop.
0
 
LVL 27

Expert Comment

by:rrz
ID: 22601342
I typed
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 27

Expert Comment

by:rrz
ID: 22601348

<br/>

Open in new window

0
 

Author Comment

by:biglarrrr
ID: 22601866
Thanks rrz@871311.

I tried using
<%
  String[] arr = {thisCategory.crumbTrail};
  request.setAttribute("myArray", arr);
%>
<c:forEach items="${myArray}" varStatus="status">
             <c:out value="${myArray[arrayLength - status.count]}"/>
</c:forEach>

but that didn't work. I didn't get any results back.
0
 
LVL 27

Accepted Solution

by:
rrz earned 500 total points
ID: 22602096
Please try this.
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>



             ${myArray[arrayLength - status.count]}
0
 

Author Closing Comment

by:biglarrrr
ID: 31501261
Thanks rrz@871311! I really appreciate your help.
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
pairs challenge 5 100
why cannot we forward request once the response is committed 2 51
couple of eclipse 5 52
hibernate example issues from command prompt 10 63
I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question