0

I have a dynamic list which is coming from a backend. I am iterating over the loop, displaying the list items in the UI. But now based on the list item value, I need to set the list display order. So how can we set the list order in the for loop?

My code is as follows:

<ul>
<c:forEach var="listObj" items="${resultList.myList}">
    <li><a>${listObj.name}</a></li>
</c:forEach>
</ul>

Here the list will always have five values. Let's assume [one, three, five, four, two]. Now based on the name of the list item I need to set the list order. So finally I need to display list items as follows: [one, two, three, four, five].

Any suggestions?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Vinod
  • 2,263
  • 9
  • 55
  • 104
  • Have a look at http://stackoverflow.com/questions/6638550/how-can-i-sort-a-list-several-different-ways-in-a-jsp – Furqan Aziz Dec 01 '15 at 17:10
  • The view isn't responsible for that. The `${resultList}` must already be sorted beforehand. Take a step back and solve it there where you prepared `resultList`, so that the view can brainlessly present it. – BalusC Dec 01 '15 at 21:20

1 Answers1

0

You'll need to write a custom sort function, so something like this:

public static List<list_obj_type> sortByAlpha(List<list_obj_type> resultList) {
        Collections.sort(resultList, new CompareResultsByAlpha());
        return resultList;
    }

Then your JSP looks like:

<c:forEach var="listObj" items="${sortByAlpha(resultList.myList)}">
Michael Angstadt
  • 880
  • 10
  • 18
  • But in my case I need to sort by using name bit that too I don't need to sort based on alpha-bate order. so instead can I do conditioning in the foreach method will that be okay? – Vinod Dec 01 '15 at 17:48