I have a list of Player objects getting passed into a JSP from a controller, and I want to display them in a couple of different ways on the same page:
- a menu sorted by name
- a list sorted by win/loss percentage
I could put separate sorted copies in the model, but dealing with different ways to display the same list seems more like a responsibility of the view, so I’d like to avoid putting the logic in the controller if I can. I already have a couple of classes implementing Comparator to help with the actual sorting.
What’s the best way to do that in a JSP?
Can I sort the list before passing it in to the different forEach tags?
The SO EL Tag Wiki describes a way to do this without using a scriptlet: using an EL function to do the sort, then using the result for the
itemsattribute in the JSTL coreforEachtag.The function class:
The TLD:
The menu JSP: