OrderByDescending of Search and Navigation does not work as expected in cms 12

Vote:
 

To display a list of pages, we built up a query and following is a part of it. 

blogsQuery = blogsQuery.OrderByDescending(x => x.LikeCount);  

Strangely, it does not sort the results in descending order of the likecount. 

However, for another filteration, it does work. The difference here is LikeCount probably returns values on the fly. Meaning it's defined as follows in the blog page. 

public virtual int LikeCount
{
    get
    {
        return LikedUsers?.Count ?? 0;
    }
}

and finally the result retrieved

 var result = blogsQuery.Take(MaximumResultsPerRequest).Skip((pageNum - 1) * MaximumResultsPerRequest).GetContentResult();

Is this a limitation of Search and Navigation ? Is there a workaround ? 

#313350
Edited, Nov 29, 2023 12:09
Vote:
 

The query is run in the Search and Navigation service, it does not have access to your code but only what has been indexed. So you either need to fetch all items and do the sorting locally. This is a bad option. Or you need to index this value as well, and update it when the likes increase. This is the better option.

#313365
Nov 29, 2023 19:45
Vote:
 

Agreed with Svante. You would have to include LikeCount in the index as a convention, 

#313410
Nov 30, 2023 10:30
Huzaim - Dec 01, 2023 11:03
Adding LikeCount as Convention worked. Thanks.
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.