Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

sort pages by cq:lastReplicated

Avatar

Former Community Member

Hi All,

I am using cq 5.6.1 and I need to sort a list of cq:Page by cq:lastReplicated date property. I want to know whats the best possible way. Also, I need to do this sorting in java class.

Thanks in advance,

Ankit

1 Accepted Solution

Avatar

Correct answer by
Level 2

You can use QueryBuilder API for this.


        ResourceResolver my_resourse_resolver;
        Session my_session;
        QueryBuilder my_builder;
        Map<String, Object> my_map;
        PredicateGroup my_predicate_group;
        Query my_query;
        SearchResult my_search_results;


        my_resourse_resolver = a_request.getResourceResolver();
        my_session = my_resourse_resolver.adaptTo( Session.class );
        my_builder = my_resourse_resolver.adaptTo( QueryBuilder.class );

        my_map = new HashMap<String, Object>();
        my_map.put( "path", a_path );
        my_map.put( "type", "cq:Page" );

        // Define sort order

        my_map.put( "orderby", "@" + jcr:content/cq:lastReplicated);
        my_map.put( "orderby.sort", "desc" );

        my_predicate_group = PredicateGroup.create( my_map );

        my_query = my_builder.createQuery( my_predicate_group, my_session );
        my_query.setHitsPerPage( a_page.getMaxResults() );
        my_search_results = my_query.getResult();

      
     

View solution in original post

5 Replies

Avatar

Correct answer by
Level 2

You can use QueryBuilder API for this.


        ResourceResolver my_resourse_resolver;
        Session my_session;
        QueryBuilder my_builder;
        Map<String, Object> my_map;
        PredicateGroup my_predicate_group;
        Query my_query;
        SearchResult my_search_results;


        my_resourse_resolver = a_request.getResourceResolver();
        my_session = my_resourse_resolver.adaptTo( Session.class );
        my_builder = my_resourse_resolver.adaptTo( QueryBuilder.class );

        my_map = new HashMap<String, Object>();
        my_map.put( "path", a_path );
        my_map.put( "type", "cq:Page" );

        // Define sort order

        my_map.put( "orderby", "@" + jcr:content/cq:lastReplicated);
        my_map.put( "orderby.sort", "desc" );

        my_predicate_group = PredicateGroup.create( my_map );

        my_query = my_builder.createQuery( my_predicate_group, my_session );
        my_query.setHitsPerPage( a_page.getMaxResults() );
        my_search_results = my_query.getResult();

      
     

Avatar

Former Community Member

Hi Jitendra,

Thank you for your inputs.

1) I want to fetch the pages based on three properties as well. cq:template, a custom page property and offTime.

2) How do I just sort a list if I already have a list of cq:pages and all I am looking for is just the sorting part.

Thanks in advance,

Ankit

Avatar

Employee

If you have a list and you want to sort it, use Collections.sort(). http://docs.oracle.com/javase/6/docs/api/java/util/Collections.html#sort(java.util.List, java.util.Comparator)

Avatar

Former Community Member

Thank you for your inputs Justin, I had thought of this option earlier but did not look much further into this since I was not sure how to write a comparator for this. So was looking for something CQ specific if it has any. However, do you have any pointers on the first point ?

Ankit