Solved

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

Posted on 2008-09-29
7
4,364 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
  • 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MaxSpan challenge 9 82
what is the main difference between git clone and fetch ann pull and push commands 2 73
session migration servlets 2 28
java jdbc batch example 8 22
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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.

867 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now