Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6121961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:50:37+00:00 2026-05-23T15:50:37+00:00

I have a list of Player objects getting passed into a JSP from a

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T15:50:38+00:00Added an answer on May 23, 2026 at 3:50 pm

    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 items attribute in the JSTL core forEach tag.

    The function class:

    package org.hierax.ifdl.tags.player;
    
    public final class PlayerSort {
        public static List<Player> sortByRank(List<Player> playerList) {
            Collections.sort(playerList, new PlayerSortByRank());
            return playerList;
        }
    
        public static List<Player> sortByAlias(List<Player> playerList) {
            Collections.sort(playerList, new PlayerSortByAlias());
            return playerList;
        }
    }
    

    The TLD:

    <?xml version="1.0" encoding="UTF-8" ?>
    <taglib 
        xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
        version="2.1">
    
        <display-name>Player Functions</display-name>
        <tlib-version>1.0</tlib-version>
        <short-name>player</short-name>
        <uri>org.hierax.ifdl.tags</uri>
    
        <function>
            <name>sortByRank</name>
            <function-class>org.hierax.ifdl.tags.player.PlayerSort</function-class>
            <function-signature>java.util.List sortByRank(java.util.List)</function-signature>
        </function>
    
        <function>
            <name>sortByAlias</name>
            <function-class>org.hierax.ifdl.tags.player.PlayerSort</function-class>
            <function-signature>java.util.List sortByAlias(java.util.List)</function-signature>
        </function>
    
    </taglib>
    

    The menu JSP:

    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%@ taglib prefix="p" uri="/WEB-INF/player.tld" %>
    
    <h1>Players</h1>
    <p>
        <c:forEach items="${p:sortByAlias(model.players)}" var="player">
            <a href="<c:url value="/player/${player.id}"/>" class="menuItem">${player.alias}</a>
        </c:forEach>
    </p>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array list of objects in my application. private static ArrayList<Player> userList=new
I have a class Player that contains a list of Accessory objects. There are
i have list of rows that user select and i want to delete them,
I have a JSP which composes a List of Objects, then renders JSP fragments
I have List I want to sort Desc by Priority, which is int and
I have List objects which are shown like this: www.mysite.com/lists/123 Where 123 is the
I have list of catalog paths and need to filter out some of them.
For example i have a player object with a list of suits of cards
For example i have a player object with a list of suits of cards
I have a list control where i want to show a string (thats fine)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.