Solved! Go to Solution.
Topics help categorize Community content and increase your ability to discover relevant content.
Views
Replies
Total Likes
Map<String, Date> arr = new TreeMap();
Date date = child.getValueMap().get("jcr:created");
arr.add(child.getName(),date);
Use this to sort Map - java - How to sort HashMap based on Date? - Stack Overflow
Map<String, Date> arr = new TreeMap();
Date date = child.getValueMap().get("jcr:created");
arr.add(child.getName(),date);
Use this to sort Map - java - How to sort HashMap based on Date? - Stack Overflow
Hi
The order of the children is the same as when they were inserted into the parent, so it is not necessarily random. If you wish to further sort the nodes, it would be plain Java code. Below is my solution, but there could be several ways to achieve the same:
Iterable<Resource> orderedChildren = StreamSupport.stream(children.spliterator(), false)
.sorted(Comparator.comparingLong(resource ->
((Calendar) resource.getValueMap().get("jcr:created")).getTimeInMillis()))
.collect(Collectors.toList());
Hope this helps.
Hi @ashwinka
As based on the jcr:created property, you can achieve this by using a Comparator to sort the children before iterating over themas based on the jcr:created property, you can achieve this by using a Comparator to sort the children before iterating over them
import java.util.Comparator;
import java.util.stream.Stream;
import org.apache.sling.api.resource.Resource;
final Iterable<Resource> children = sectionResource.getChildren();
Stream<Resource> sortedChildren = StreamSupport.stream(children.spliterator(), false)
.sorted(Comparator.comparing(resource -> resource.getValueMap().get("jcr:created", Long.class)));
sortedChildren.forEach(child -> {
// Your processing logic for each child
});
Comparator.comparing() is used to create a comparator based on the jcr:created property. This comparator is then applied to the stream of resources using the sorted() method.
Make sure to replace "jcr:created" with the actual property name if it's different in your case.
JCR nodes can be of 2 types: ordered or unordered. In the case of ordered nodes child nodes are always returned in the same order. In the case of unordered nodes child nodes the order is not guaranteed.
Most types are ordered, especially when dealing in areas where order matters. In case it does not, unordered nodes can be used which can bring a performance benefit.
Views
Likes
Replies
Views
Like
Replies