Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

How to use ResourceSorter

Avatar

Level 2

I want to use sort() method from https://developer.adobe.com/experience-manager/reference-materials/6-5/javadoc/com/day/cq/dam/common... 

i have all the required resources stored in variable children as given below:

final Iterator<Resource> children = sectionResource.listChildren();

 

How can i can sort children according to property "jcr:created"

 

if there is a better approach than using ResourceSorter , please mention that also. Also show an example if possible

thank you.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@ashwinka: One of the solution would be to sort these using collection sort.

Please refer: https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/best-way-to-sort-a-list-of...
With ResourceSorter have you tried passing 'created' for the column method parameter.

As per documentation, you can sort using any of the columns specified at Service to sort Resources by a column defined in- /libs/dam/gui/content/commons/availablecolumns
Get a reference of 'ResourceSorter' and then call the sort method by passing the appropriate params values.

resources - Resources to sort i.e children 
column - Column used as sort criteria i.e created
reverse - If true result gets reversed false (unless you want in the reverse order of jcr:created)
limit - Max number of resources to return (as applicable)
offset - Number of first resources not to return (would be 0, but you can try)
filters - node or mime types

View solution in original post

4 Replies

Avatar

Correct answer by
Community Advisor

@ashwinka: One of the solution would be to sort these using collection sort.

Please refer: https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/best-way-to-sort-a-list-of...
With ResourceSorter have you tried passing 'created' for the column method parameter.

As per documentation, you can sort using any of the columns specified at Service to sort Resources by a column defined in- /libs/dam/gui/content/commons/availablecolumns
Get a reference of 'ResourceSorter' and then call the sort method by passing the appropriate params values.

resources - Resources to sort i.e children 
column - Column used as sort criteria i.e created
reverse - If true result gets reversed false (unless you want in the reverse order of jcr:created)
limit - Max number of resources to return (as applicable)
offset - Number of first resources not to return (would be 0, but you can try)
filters - node or mime types

Avatar

Community Advisor

Hi @ashwinka,

ResourceSorter is a dedicated OSGi service that can be used only against DAM asset/collections it will not work for resources representing Pages - so please keep that in mind.

The purpose of this service is either return Comparator or to sort given Resources base on Compartor correlated with a column. List of Comparators base on nodes can be found under below location

/libs/dam/gui/content/commons/availablecolumns

Screenshot 2024-01-19 at 09.59.45.png

In other words if you want to sort Resources by create date you should use create column.

ResourceSorter is mainly used to handle interactions with Assets GUI.

Code example:

 

@Refernece
private ResourceSorter resourceSorter;

// ...

// This has to be Assets only
Iterator<Resource> resourceIterator = root.listChildren();
resourceSorter.sort(resourceIterator, "created", false, 100, 0, null);

 

In terms of sorting resources, you can use Java api for this. Here is an example:

 

Iterator<Resource> resourceIterator = root.listChildren();
List<Resource> resourceList = new ArrayList<Resource>();

while (resourceIterator.hasNext()) {
    resourceList.add(resourceIterator.next());
}

Collections.sort(resourceList, new ResourceComparator())

class ResourceComparator implements Comparator<Resource> {

    @Override
    public int compare(Resource r1, Resource r2) {
        if (r1 != null && r2 != null) {
            Calendar c1 = r1.getValueMap().get("jcr:created", Calendar.class);
            Calendar c2 = r2.getValueMap().get("jcr:created", Calendar.class);
            return c1.compareTo(c2);
        }
        return 0;
    }
}

 

Above Java code is node type agnostic so it's free of limitation comparing to ResourceSorter.