Customizing Blacklight Tutorial Series (v5.14.0)

Sort Fields

Blacklight provides a dropdown UI element for changing the sort order of search results. The sort options will show up in the dropdown in the order they’re configured, and the first one is the default sort if no sort key is present in the url.

Sort dropdown
Default sort dropdown

I would recommend explicitly adding a key (year in the example below) so that the sort fields are changeable without breaking existing/bookmarked urls.

# app/controllers/catalog_controller.rb
config.add_sort_field('year') do |field|
  field.sort = 'pub_date_sort desc, title_sort asc'
  field.label = 'pub. date'
end
Sort dropdown
Updated sort dropdown

The pub. date sort option will pass sort=year in the url and the logic of what a year sort means is held in the sort field configuration (and can be changed without affecting the url parameters).

You can now customize your sort options as well as mask the solr fields that are being sorted on in order to prevent breaking bookmarked urls if the fields change.