Solved

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

Posted on 2008-09-29
7
4,485 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
Industry Leaders: 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

Industry Leaders: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
why cannot we forward request once the response is committed 2 55
servlet example 11 77
Eclipse with gitlab 1 268
What kind of framework must i use to do that ? 2 48
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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.

710 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