Sitecore Solr provides an abundance of advanced search features, but even without implementing anything as advanced as stemming, or search term suggestions, Solr’s default algorithm provides excellent relevancy in the search results. We’ve implemented Sitecore Solr in a number of complex projects like the student-centered website for California Community Colleges, the Department of Water Resources website and Intranet, and the multi-organizational Department of General Services.

Alphabetical listing of items such as navigation items or search filters is one of the easiest ways for people to navigate and find what they are looking for in a long list of terms.

However, the search filters returned from Solr are not sorted alphabetically.

Instead of the natural language terms, Solr returns a list of facet IDs in the order that Solr internally determined based on relevancy.

Below is an example of the Solr Search data structure, which makes sense to the algorithm, but not necessarily to the website visitors that use search filters.

[0]: "9955d3531ccb4e6e8a92c7e484d329ca"
[1]: "dac1e325382d48569cabea48c83165cd"
[2]: "e56edf635d4048a098bcbeaef8541975"
[3]: "ef1770d934f54b6b821d29be800667b9"

Looking at the example above, items are represented as Sitecore GUIDs. Applying an ascending order operation to the list above won’t make sense, because at this point we don’t know what’s the actual user-friendly label associated with each ID.

To apply meaningful sorting to Solr search results, we have to do a little bit of preparation.

  1. Make a new list.
  2. Translate each ID into the corresponding Sitecore Item and add it to the newly created list.
  3. Find out each item’s associated title.
  4. Sort the new list based on the associated title.

Translated to the code language, it looks like the example below:

ret = asc ? ret.OrderBy(x => x["Title"]).ToList() : ret.OrderByDescending(x => x["Title"]).ToList();
List ret = new List();
foreach (var filter in allFilters) {
    Item filterItem = Sitecore.Context.Database.GetItem(new Sitecore.Data.ID(filter));
    ret.Add(filterItem);
}

I hope this helps you sort the returned facets. For a live example, please visit CCC’s Custom Document Search.

As always, feel free to reach out to us with feedback and further questions.