Expand my Community achievements bar.

Submissions are now open for the 2026 Adobe Experience Maker Awards.

Sort graphql results by Date

Avatar

Level 2

I am trying to sort the feed generated using graphql query by date. But the results are not as expected. 
Sample Query:

query {
ContextList(
filter: {tags: {_expressions: [{value: "content-type/story"}]}}
sort: "DisplayDate DESC"
) {
items {
title
_tags
DisplayDate
publisheddate
pageName
}
}
}

 

But the result is not sorted. Any guidance.

Sample result:

{
"data": {
"ContextList": {
"items": [
{
"DisplayDate": "2025-04-03"
},
{
"DisplayDate": "2025-04-01"
},
{
"DisplayDate": "2025-03-20"
},
{
"DisplayDate": "2025-03-17"
},
{
"DisplayDate": "2025-03-13"
},
{
"DisplayDate": "2025-03-12"
},
{
"DisplayDate": "2025-03-10"
},
{
"DisplayDate": "2025-03-10"
},
{
"DisplayDate": "2025-03-04"
},
{
"DisplayDate": "2025-02-26"
},
{
"DisplayDate": "2025-02-24"
},
{
"DisplayDate": "2025-02-19"
},
{
"DisplayDate": "2025-02-13"
},
{
"DisplayDate": "2025-02-13"
},
{
"DisplayDate": "2025-02-06"
},
{
"DisplayDate": "2025-02-05"
},
{
"DisplayDate": "2025-02-04"
},
{
"DisplayDate": "2025-02-04"
},
{
"DisplayDate": "2025-02-04"
},
{
"DisplayDate": "2025-01-31"
},
{
"DisplayDate": "2025-01-31"
},
{
"DisplayDate": "2025-01-29"
},
{
"DisplayDate": "2025-01-27"
},
{
"DisplayDate": "2025-01-21"
},
{
"DisplayDate": "2025-01-16"
},
{
"DisplayDate": "2024-12-29"
},
{
"DisplayDate": "2024-12-19"
},
{
"DisplayDate": "2024-12-18"
},
{
"DisplayDate": "2024-12-18"
},
{
"DisplayDate": "2024-12-18"
},
{
"DisplayDate": "2024-12-18"
},
{
"DisplayDate": "2024-11-22"
},
{
"DisplayDate": "2024-10-29"
},
{
"DisplayDate": "2024-10-04"
},
{
"DisplayDate": "2024-10-03"
},
{
"DisplayDate": "2024-10-03"
},
{
"DisplayDate": "2024-10-03"
},
{
"DisplayDate": "2024-09-19"
},
{
"DisplayDate": "2024-09-03"
},
{
"DisplayDate": "2024-08-16"
},
{
"DisplayDate": "2024-08-13"
},
{
"DisplayDate": "2024-08-12"
},
{
"DisplayDate": "2024-08-07"
},
{
"DisplayDate": "2024-08-06"
},
{
"DisplayDate": "2024-07-31"
},
{
"DisplayDate": "2024-07-31"
},
{
"DisplayDate": "2024-07-31"
},
{
"DisplayDate": "2024-07-25"
},
{
"DisplayDate": "2025-07-03"
},
{
"DisplayDate": "2025-07-03"
}
]
}
}
}

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

5 Replies

Avatar

Community Advisor

Hi @Harsharya 

 

Since the sort: "DisplayDate DESC" parameter isn’t sorting the results as expected in your GraphQL query, likely due to an issue with AEM’s GraphQL implementation, the best immediate solution is to fetch the data without server-side sorting and sort it client-side. 


You can try this. 

query {
  ContextList(
    filter: {tags: {_expressions: [{value: "content-type/story"}]}}
  ) {
    items {
      title
      _tags
      DisplayDate
      publisheddate
      pageName
    }
  }
}

 

const items = response.data.ContextList.items;
const sortedItems = items.sort((a, b) => new Date(b.DisplayDate) - new Date(a.DisplayDate));

 

This ensures your feed is sorted by DisplayDate in descending order, starting with the latest dates (e.g., "2025-07-03") and ending with the earliest (e.g., "2024-07-25").

For a long-term fix, consider investigating the AEM GraphQL setup.

Avatar

Level 1

Hi @partyush 

Thanks for your answer.

Could you please explain,  (likely due to an issue with AEM’s GraphQL implementation) we created content fragments and DisplayDate is the field with datatype as date. Any specifics will be really helpful.

Avatar

Community Advisor

Hi Sure,  @HarshVa2

The field name specified in the sort parameter must exactly match the field name defined in your content fragment model in Adobe Experience Manager (AEM). GraphQL is case-sensitive.

Your query is:
query {
ContextList(
filter: {tags: {_expressions: [{value: "content-type/story"}]}}
sort: "DisplayDate DESC"
) {
items {
title
_tags
DisplayDate
publisheddate
pageName
}
}
}


You expect the items to be returned with DisplayDate sorted in descending order (latest dates first). However, in the sample result, the dates start with "2025-04-03", followed by "2025-04-01", then drop to "2025-03-20", and so on, until near the end where "2025-07-03" appears a date that should be among the first if sorted correctly in descending order. This indicates that the sort: "DisplayDate DESC" parameter is either not being recognized or not functioning as intended in AEM’s GraphQL implementation.

Since the server-side sorting isn’t working as expected, the most practical and immediate solution is to fetch the data without relying on the server-side sort parameter and perform the sorting client-side in your application. This ensures you get the desired order regardless of server behavior.

 

You can keep the query as is or remove the sort parameter since it’s not effective:

query {
ContextList(
filter: {tags: {_expressions: [{value: "content-type/story"}]}}
) {
items {
title
_tags
DisplayDate
publisheddate
pageName
}
}
}


Assuming you’re working in a JavaScript environment (common with AEM front-end integrations), you can sort the returned items array based on the DisplayDate field. Since the dates are in "YYYY-MM-DD" format, they can be parsed as Date objects for accurate chronological sorting.

// Assuming 'response' contains the GraphQL query result
const items = response.data.ContextList.items;

// Sort items in descending order by DisplayDate
const sortedItems = items.sort((a, b) => {
  return new Date(b.DisplayDate) - new Date(a.DisplayDate);
});

// Now 'sortedItems' is sorted with the latest DisplayDate first
console.log(sortedItems);
  • Explanation:
    • new Date(b.DisplayDate) - new Date(a.DisplayDate) returns a positive number if b’s date is later than a’s, placing b before a in the array (descending order).
    • The "YYYY-MM-DD" format is directly compatible with the Date constructor, so no additional parsing is needed.

Example Output

After applying the sort to your sample result, the first few items would be:

  • "2025-07-03" (first occurrence)
  • "2025-07-03" (second occurrence)
  • "2025-04-03"
  • "2025-04-01"
  • "2025-03-20"
  • ...and so on, down to "2024-07-25".

This matches the expected descending order.

Thanks.

Avatar

Level 1

Thanks @partyush ,

 

I have made sure the propertyname is same is with graphql query. The problem is when I changing it datatime It starts sorting but when I am using date Its not working out. Yes, through Js we can sort. But what can be the cause thats it not sorting in graphql

Avatar

Administrator

@Harsharya Just checking in — were you able to resolve your issue?
We’d love to hear how things worked out. If the suggestions above helped, marking a response as correct can guide others with similar questions. And if you found another solution, feel free to share it — your insights could really benefit the community. Thanks again for being part of the conversation!



Kautuk Sahni