Reverse order using Struts iterate tag  
Author Message
Wendy S





PostPosted: 2003-7-19 3:48:00 Top

java-programmer, Reverse order using Struts iterate tag "Bob" <email***@***.com> wrote
> I am fairly new to struts. Is it possible to iterate backward through
> a collection when using the struts iterate tag? We would like to list
> the values in reverse order on the page.

Yes, I've got things listing in reverse date order in my own Struts webapp.
However, I don't think this has anything to do with Struts. <logic:iterate>
and <c:forEach> are going to return the items in whatever order they come
out of the Iterator returned by the Collection's iterator() method.

To make things sort "backwards", you can override the 'compareTo' method
(and probably equals and hashCode, to be completely correct) on the class of
the objects in your collection. Then call Collections.sort with your
Collection. (I think you can also use a Comparator instead of overriding
compareTo, but that's not how I did it. Actually, looking through my code I
think I may have sorted these on the database side before returning the data
to the DAO! But it's still possible.)

Then when you get to the JSP the logic:iterate tag should display things in
reverse order.

Can you post the JSP and let us know what type of objects you have in your
Collection?

--
Wendy in Chandler, AZ