order by custom jcr property name in Query Builder | Community
Skip to main content
Level 7
February 11, 2024
Solved

order by custom jcr property name in Query Builder

  • February 11, 2024
  • 3 replies
  • 1201 views

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 @5644378: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.

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 Imran__Khan

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.

3 replies

Imran__Khan
Community Advisor
Imran__KhanCommunity AdvisorAccepted solution
Community Advisor
February 11, 2024

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.

SureshDhulipudi
Community Advisor
Community Advisor
February 11, 2024

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);

Level 7
February 12, 2024

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. 

joerghoh
Adobe Employee
Adobe Employee
February 11, 2024

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