Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thymeleaf: How to loop through a list in inverse order?

I have a list called 'notifications' and I use Thymeleaf 'each method' to access its elements one by one. I can do this successfully as below.

<li th:each="n : *{notifications}"> 
    <h4 type="text" th:text="*{n.message}"></h4>
</li>

note: 'message' is the attribute I need to retrieve from the list.

How can I access the elements in the reverse order? For an example if this is my current output,

Cat
Dog
Rat

How can I get output as this?

Rat
Dog
Cat
like image 380
sndu Avatar asked Jan 04 '23 09:01

sndu


1 Answers

I would reverse it server side, if possible. If you don't want to do that, maybe something like this would work for you:

<li th:each="i : ${#numbers.sequence(notifications.size() - 1, 0, -1)}"> 
    <h4 type="text" th:text="${notifications[i].message}"></h4>
</li>
like image 95
Metroids Avatar answered Jan 14 '23 11:01

Metroids