Search code examples
htmlsql-order-bythymeleaf

Order by in Thymeleaf


I need show this foreach by order Asc, how can I do it?

<tr th:each="ment : ${mentor}" th:if="${ment.jobId == job.id}">
    <td th:text="${ment.id}"></td>
    <td th:text="${ment.name}"></td>
    <td th:text="${ment.qtyMentee}"></td>
    <td th:text="${ment.jobId}"></td>
</tr>

Solution

  • This could be achieved with sort utility methods for lists, described here.

    /*
     * Sort a copy of the given list. The members of the list must implement
     * comparable or you must define a comparator.
     */
    ${#lists.sort(list)}
    ${#lists.sort(list, comparator)}
    

    Example

    <tr th:each="ment : ${#lists.sort(mentor)}">