OrderByDescending and Date fields
We're trying to sort on a datefield in our search results, but it's not working. Here's the code:
var results = context.GetQueryable<SearchResultItem>().Where( item => item.Paths.Contains(articleFolderId) && item["categorylist"] == topicID && item.Language == Sitecore.Context.Language.Name).ToList(); results = results.Where(x => x.CreatedDate != null).OrderByDescending(y => y.CreatedDate).ToList();
We've added this field in the fieldMap section of the search provider config.
<fieldType fieldName="CreatedDate" isFacet="false" isMultiValue="false" isSortable="true" settingType='Coveo.Framework.Configuration.FieldConfiguration, Coveo.Framework'/>
Is there something we're missing?
I guess you should call the
.OrderByDescending() method before doing the first call to
.ToList(). With your actual code, the first call to
.ToList() will call the search index and retrieve the results. Note that, with Coveo, the index will return 10 results only. When you call
.ToList() the second time, the results are coming from an internal list, not the search index. So it sorts only the 10 results that were returned previously.
I hope this helps
The thing is we have a custom field to order the collection, called "createddate". We tested the solution of ordering before the call to the ToList() method and still the same order. Also we thought it was something related to the DateTime type but with the .Ticks property is still the same order. We looked into the CES Admin Tool > Configuration and the field is set to IsSortable true. Any thoughts?
Thanks! Regards, Sergio