get nodes ordered according to jcr:created | Community
Skip to main content
ashwinka
Level 2
January 17, 2024
Solved

get nodes ordered according to jcr:created

  • January 17, 2024
  • 3 replies
  • 944 views
final Iterable<Resource> children = sectionResource.getChildren();
 
for (final Resource child : children) {
 
}
 
 
here we are getting child in random order. How can i get the child ordered according to jcr:created  time inside the for loop
This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by yashp0808

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

 

 

3 replies

yashp0808
yashp0808Accepted solution
Level 3
January 17, 2024

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

 

 

EstebanBustamante
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
January 17, 2024

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.

Esteban Bustamante
Raja_Reddy
Community Advisor
Community Advisor
January 17, 2024

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.