Sitecore Content Search API sorting - ThenBy not evaluating correctly

I'm going to do a few posts about Content Search - the new Lucene search API dubbed Linq to Sitecore by some.

However let's kick off with a quick problem which I had ordering my search results.

With linq we could do something like this:

List<SearchResult> test = new   List<SearchResult>();

test.Add(new SearchResult() { Header = "AAA", CreatedDateSmall = "20091010" });
test.Add(new SearchResult() { Header = "AAA", CreatedDateSmall = "20091110" });
test.Add(new SearchResult() { Header = "AAA", CreatedDateSmall = "20091210" });
test.Add(new SearchResult() { Header = "BBB", CreatedDateSmall = "20091010" });

test = test.OrderBy(i =>     i.Header).ThenByDescending(j => j.CreatedDateSmall).ToList();

Which would give:

AAA 20091210
AAA 20091110
AAA 20091010
BBB 20091010

However a similar approach using the Content Search API:

var items = context.GetQueryable<SearchResult>();

items = items.OrderBy(i => i.Topic).ThenByDescending(j => j.DisplayDateText);

Does not work.

The problem is that the linq methods are called in reverse order and has been registered as a bug (7.1 has just been released at time of writing).

To resolve swap your lamba expressions:

items = items.OrderByDescending(i => i.DisplayDateText).ThenBy(j => j.Topic);

Dave Leigh

Web, and long time Sitecore developer based in Bristol, UK, working at Valtech - valtech.co.uk - @valtech.
I occasionally do other things too.