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
Solved

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

Posted on 2008-09-29
7
4,416 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
mapAB Challlenge 35 156
github account with ecipse 1 52
ejb on wildfly 5 30
ejb entity bean example issue 2 16
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…
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…
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.

839 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