How to use ResourceSorter | Community
Skip to main content
ashwinka
New Participant
January 19, 2024
Solved

How to use ResourceSorter

  • January 19, 2024
  • 3 replies
  • 914 views

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

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.

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 Kamal_Kishor

@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-resources/m-p/164356
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

3 replies

lukasz-m
New Participant
January 19, 2024

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

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.

yashp0808
New Participant
January 19, 2024
Kamal_Kishor
Kamal_KishorAccepted solution
New Participant
January 19, 2024

@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-resources/m-p/164356
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

Kamal_Kishor
New Participant
January 19, 2024