Expand my Community achievements bar.

SOLVED

get nodes ordered according to jcr:created

Avatar

Level 2
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
Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Level 4

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

 

 

View solution in original post

3 Replies

Avatar

Correct answer by
Level 4

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

 

 

Avatar

Community Advisor

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

Avatar

Community Advisor

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.