Views sort criteria

Sort criteria are the rules in a Drupal View that set the order of its results, such as newest first or by title. A view can apply several.

Learn DrupalDrupal glossary

Sort criteria are the settings in a Drupal View that decide the order of its results. Each criterion names a field to sort on and a direction, ascending or descending. A view can hold several criteria and applies them in the order they are listed: the first sets the main order, and each later one orders the items that are equal on the ones before it.

How sort criteria work in Drupal 11

The Drupal User Guide counts sort criteria among the parts of a view: they define the order of the output and can be based on any content field. On the view edit screen they have their own section, Sort criteria, with an Add and a Rearrange link. After you pick a field, the dialog asks for Sort ascending or Sort descending.

In code, each criterion is a sort handler plugin that extends SortPluginBase. Its query() adds an ORDER BY clause using the order option, which is ASC until you change it. The SQL query plugin keeps the clauses in the order they were added and applies them in sequence. A database sorts on the first column, then on the next one within each group of equal values; the MySQL manual shows this with ORDER BY species, birth DESC.

Two plugins behave differently from the standard one:

  • Date sort. It has a granularity setting: second, minute, hour, day, week, month or year. Two dates inside the same unit count as the same date, so the next criterion decides between them. The code default is second.
  • Random sort. It hides the direction choice and adds a random order. Its cache max age is 0, so the result is not cacheable.

A criterion can also be exposed. The Expose sort button turns it into a control for visitors, with a label and a Sort field identifier. The identifier is the value of the sort_by URL parameter and may contain letters, digits and ., -, _ and ~.

Example: a news list, newest day first

In a view of articles, add Authored on from the Content category, set it to Sort descending and set the granularity to Day. All articles from one day now count as equal. Add Title as a second criterion with Sort ascending. The newest day comes first, and the articles inside each day appear in alphabetical order.

Common mistakes

  • Wrong criterion order. The first criterion wins. Use Rearrange to move the one that matters most to the top.
  • Changing every display by accident. Sort criteria are one of the sections a display can override. The For list in the dialog decides whether you change all displays or only the current one, as in the User Guide's block example, which uses This block (override).
  • Forgetting table click sorting. In a table format, the setting Override normal sorting if click sorting is used controls whether the view's own criteria still apply after a visitor sorts by a column.
  • Using a random sort without checking caching. The random sort reports a cache max age of 0, so the view's result cannot be cached. Test the page before you ship it.

For marketing and content teams

Agree on the ordering rule before the view is built. "Newest first" sounds clear, but it matters which date is meant. If a view sorts by the date a page was last changed, every edit moves that page to the top. If it sorts by the authored date, edits do not change the position. Tell your developer which behavior you want.

With DXPR

DXPR Builder can place a view display on a page. The list appears with the filters, sorting and style that the view defines, so you change the order in the view, not on the page. See DXPR Builder.

Sources

Questions about Views sort criteria

Add a date sort, such as Authored on, to the Sort criteria section and choose Sort descending. Descending puts the latest date at the top. Ascending would put the oldest first.

Yes. Open the sort criterion and use the Expose sort button. Visitors then get a control labeled with the text you set, and the choice travels in the page address as the sort_by parameter.

A display can carry its own sort criteria. If you edited the block with This block (override) selected, the page keeps its old sorting. If you picked All displays, every display that has not overridden sorting changed too.

Related terms

All Drupal terms from A to Z