I have such snippet of code, I would like to change the way of sorting (now is sortByTime) but for instance i want to change for sortByTitle or sortById how can i do this?
Exist class with my methods of sorting enity (omitting for brevity some methods)
public final class ListEntitySort {
private static Comparator<Entity> compareByStartTime = Comparator.comparing(Entity::time);
public static List<Event> sortByTime(List<Event> events) {
events.sort(compareByStartTime);
return events;
}
... more
}
FOR this sort I use function.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">
<tlib-version>1.0</tlib-version>
<short-name>Entity_Functions</short-name>
<uri>ua.com.example</uri>
<function>
<name>sortByTime</name>
<function-class>ua.com.example.web.command.functions.ListEntitySort</function-class>
<function-signature>java.util.List sortByTime(java.util.List)</function-signature>
</function>
<function>
<name>formatLocalDateTime</name>
<function-class>ua.com.example.web.command.functions.Dates</function-class>
<function-signature>java.lang.String formatLocalDateTime(java.time.LocalDateTime, java.lang.String)
</function-signature>
</function>
</taglib>
in serlvet I obtain and set attribute
List<Entity> entities = someService.getListEntities();
request.setAttribute("entities", entities);
<c:forEach var="entity" items="${p:sortByTime(entities)}">
<tr>
<td>${entity.id}</td>
<td>${entity.title}</td>
<td>${entity.description}</td>
<td>${entity.location}</td>
<td>${p:formatLocalDateTime(entity.time, 'dd-MM-yyyy HH:mm')}</td>
<td>
</tr>
Probably create a flag that will send the sorting method to the servlet and change the sorting method parameter in for each. or maybe add scriplet in jsp<% %> or in jsp make something with if tag via jstl
<c:if test="${check the way of sorting }">
put here for each
</c:if>