how to sort list of Page on basis of (title, template, lastModified) | Community
Skip to main content
Level 4
December 13, 2021
Solved

how to sort list of Page on basis of (title, template, lastModified)

  • December 13, 2021
  • 1 reply
  • 1038 views

Hi @1905403

 

I have List<Page> i need to do a ordering just like a list Component which uses Order By.

How i can achieve it?

 

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 lukasz-m

Hi @lone_ranger, sorting lost of pages can be done in similar way to sorting list of any other java object, i.e you can implement Comparator. Below you can find simple Comparator implementation that will allow you to sort list of Pages by page title (jcr:title).

import com.day.cq.wcm.api.Page; 
import java.util.Comparator;
import java.util.Collections;
import java.util.List;

// List with pages
List<Page> pages

// sorting list of pages using custom comparator
Collections.sort(pages, new PageByTitleComparator());

// java comparator implementation
class PageByTitleComparator implements Comparator<Page> {
 
    @Override
    public int compare(Page p1, Page p2) {
        return p1.getTitle().compareTo(p2.getTitle());
    }
} 

 

1 reply

lukasz-m
Community Advisor
lukasz-mCommunity AdvisorAccepted solution
Community Advisor
December 16, 2021

Hi @lone_ranger, sorting lost of pages can be done in similar way to sorting list of any other java object, i.e you can implement Comparator. Below you can find simple Comparator implementation that will allow you to sort list of Pages by page title (jcr:title).

import com.day.cq.wcm.api.Page; 
import java.util.Comparator;
import java.util.Collections;
import java.util.List;

// List with pages
List<Page> pages

// sorting list of pages using custom comparator
Collections.sort(pages, new PageByTitleComparator());

// java comparator implementation
class PageByTitleComparator implements Comparator<Page> {
 
    @Override
    public int compare(Page p1, Page p2) {
        return p1.getTitle().compareTo(p2.getTitle());
    }
}