Expand my Community achievements bar.

July 31st AEM Gems Webinar: Elevate your AEM development to master the integration of private GitHub repositories within AEM Cloud Manager.
SOLVED

order by custom jcr property name in Query Builder

Avatar

Level 8

Hello Everyone,

 

I am trying to order my content based on last modified  order in descending.  Below code works fine

Map<String, String> param = new HashMap<>();
param.put("p.limit", 1);
param.put("p.guessTotal", "true");
param.put("type", "cq:PageContent");
param.put("path", "/content/abc/myarticles/en");
param.put("2_property", "customType");
param.put("2_property.1_value", "article");
param.put("orderby", "@cq:lastModified");  //  this is of type: Date, This works fine.
param.put("orderby.sort", "desc");

 

Now, instead of @cQ:lastModified field, I am trying to use custom field: customPublishTime

param.put("orderby", "@customPublishTime");

sample value of customPublishTime is:  2022-03-04T00:00:00.000+05:30  This is of type: String.

But this is not working. Since, for some reason, I cannot modify the type of customPublishTime from String to Date.

So, while writing the code for Hashmap Parameter, is it possible to convert the custom field type to Date?

 

Thanks in advance.

1 Accepted Solution

Avatar

Correct answer by
Level 10

Current sorting will allow you to sort customPublishTime as a string which is of no use.

 

Create a custom script and try to convert field into date data type and than fire query to apply sorting.

View solution in original post

4 Replies

Avatar

Correct answer by
Level 10

Current sorting will allow you to sort customPublishTime as a string which is of no use.

 

Create a custom script and try to convert field into date data type and than fire query to apply sorting.

Avatar

Community Advisor

you have to convert String type to Date type using SimpleDateFormat JAVA class and then pass date type to query.

 

import java.text.SimpleDateFormat;
import java.util.Date;

String customPublishTime = "2022-03-04T00:00:00.000+05:30";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
Date date = format.parse(customPublishTime);

Avatar

Level 8

Hi @SureshDhulipudi 

 

I think, this solution may not work in my case. Since, what my query does is:  Get latest page of type article, in descending order, where as customPublishTime is a String field.

Your solution might work, if I get a particular page and then convert the String field to Date field. 

Avatar

Employee Advisor

this property has to have the type Date, otherwise the ordering is not what you want.