I am looking for a flexible way of sorting the results from a search in a Habitat structured solution. I tried different approaches but couldn't find one that doesn't need a hardcoding way of doing things.

Let's say I have to sort by some types like 'Date' and 'Title', both can be ascendant and descendant. Since a solution contains multiple page types, like News, Events, Articles, Publications, each of this could have its own project in solution and has its own fields in Sitecore to sort on, example on News you would have 'Date', on Events would be 'Start Date', on Articles could be 'Updated Date' on Publications could be 'Release Date' and so on.

I am looking for a way I could define these in that specific project, and then by using the Foundation.Indexing project to dynamically sort each depending on type and on sort params I send.

I am doing this on a Sitecore Habitat structure and trying to make it flexible enough so if I add later new projects I don't need to modify the actual search but extend it in the added project. I have tried different ways of doing it but haven't managed so far without having to hardcode some field names or instances that would make me do changes each time I add new projects to the solution.

If anyone has any idea how could this be done in a nice and flexible way let me know. I am eager to find out if anyone ever tried something like this on Habitat and managed to have it done in a good pro way.

2 Answers 2


I think you need to put your base abstract search class to foundation project. then add implementation of this abstract class to feature project and override methods which you need. in your case just add some method which will complete ordering.

if you use base habitat (helix implementation example) search service you can see example of filtering in NewsRespository. Search Service return ISearchResults which contain collection of ISearchResult. So I can not see any generic methods in this process. You can not set up flexible ordering (x => x.Date or x => x.Title) without using directly types.

My suggestion updates existing search service to using generic or cast search service results to necessary types in repository layer then ordering by field.

do not forget that habitat just an example using Helix principles.


I am not quite sure about Habitat itself, but assuming you have sort of generic search service, you can do something like that (method signature is simplified):

 public IQueryable<TResult> Search<TResult, TOrderKey>(string q, Expression<Func<TResult, TOrderKey>> order, bool desc = true)
            // searching, getting IQueryable
            // the below is under the search context as well
            if (order != null)
                queryableResult = desc ? queryableResult.OrderByDescending(order) : queryableResult.OrderBy(order);

            //close the search context and return
            return queryableResult;

Then you can call it from your code like that:

var results = this.SearchService.Search<TMyResultItem, DateTime>("test query", (x) => x.Date, false);

Assuming that TMyResultItem contains a Date field of the DateTime type.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.