Sorting

Sorting allows users to quickly re-organize similar content on a long list when the default order may be insufficient for users to scan the data set.

Examples

Sorting
Loading live example...

View in Pajamas UI Kit →

Structure

TODO:
Add structure image. Create an issue

Guidelines

When to use

  • Use the sorting component when the list has pagination and spans multiple pages, as it can make it easier for users to digest and find relevant content.
  • A user's sort selections are meant to be persisted. If they make a sort selection and then leave the page, when they return to the page, their previous sort selections will be preserved.
  • Sorting can occur in tables and in lists. While sorting in lists relies on the sorting component described on this page, sorting within a table happens in the table header. See table sorting guidelines.

When not to use

  • When using the sorting component, make sure the list in the dropdown doesn't contain more than 10 items, otherwise it will create usability problems. See combobox guidelines.
  • If a list is static, easy to search/scan, or where the data set won't grow, the sorting component is unnecessary.

Appearance

  • The sorting component is comprised of two parts: a dropdown, and a sort direction icon button.

Sizes

Use the size prop to render a small variant of the dropdown toggle and sort direction button, for use in constrained spaces such as a settings drawer.

Small size
Loading live example...

Behavior

  • The dropdown allows users to choose their sorting criteria, whether it be by label, date, or popularity.
  • As soon as an item in the dropdown is selected, the list content re-sorts automatically.
  • The sort dropdown allows for sorting by a single selected item.
  • The sort direction icon button allows users to change the direction of the sort, from ascending to descending or vice-versa.
  • Avoid using invisible attributes for sorting because it lacks visual reinforcement of the user's action, which can be confusing.
  • The default sort order on a list is determined by the designer on a page-by-page basis.
  • When only one sort option is relevant (for example, a board that only supports manual ordering), you can choose to disable the sorting component with the disabled prop instead of hiding it, so users can still see how the list is sorted.

Accessibility

  • The sort dropdown uses a listbox pattern and announces "Sort by" plus the button text.
  • The sort direction toggle button must have an accessible name that reflects the current direction and the action it will perform. For example, Sort ascending or Sort descending. The component automatically generates this label from the current sort direction; use the sortDirectionToolTip prop to override it with a custom accessible name.
  • When a sort option disables the direction toggle (for example, Most relevant), the button is marked aria-disabled and retains an accessible name explaining why it is unavailable, so keyboard and screen reader users are informed.
  • When the disabled prop disables the whole control, the direction toggle's accessible name changes to Sorting is unavailable for <sort field> (for example, Sorting is unavailable for Manual) instead of describing a direction. Screen reader users learn both that sorting is unavailable and how the list is currently sorted.
  • After a sort selection is made and the list re-sorts, announce the change to screen reader users with a live region when the update is not immediately obvious from the surrounding context.
  • Keyboard navigation is inherited from GlCollapsibleListbox. See dropdown combobox accessibility guidelines.

Code reference

GlSorting

The sorting component allows the user to select the field on which they would like to sort a list and whether to sort in ascending or descending order.

Provide a list of sort options via the sortOptions prop with this structure:

type sortOptionsProp = Array<{
  value: string;
  text: string;
  directionToggleDisabled?: boolean;
}>;

The value should be a unique primitive value, and text is the user-facing string for that option.

Set the currently selected sort option by passing a value to the sortBy prop. This should equal one of the sortOptions values. The selected sort option is rendered with a check mark next to it in the dropdown menu.

When the user changes the selected sort option, a sort-by-change event is emitted, with the value of the option as the only payload.

The text of the dropdown trigger button is the text of the selected sort option. Pass a string to the text prop to override this behavior.

When the user clicks on the sort direction button, a sort-direction-change event is emitted, with a boolean value as its only payload. If the payload is true, the new order is ascending; otherwise it's descending.

The sort direction button's tooltip and aria-label are automatically derived from the current sort direction. Pass a string to the sortDirectionToolTip prop (String, default: null) to override both with a custom label.

Some sort options have no ascending/descending meaning, such as a "Most relevant" sort. To disable the sort direction button for these, set directionToggleDisabled: true on the relevant sortOptions entries; the button is then automatically disabled whenever such an option is selected.

When disabled, the button stays visible to avoid layout shifts and focus loss when switching sort options, but it is marked aria-disabled, keeps an accessible name explaining why it is unavailable, and no longer emits sort-direction-change.

To disable the entire control (dropdown and toggle), set the disabled prop (Boolean, default: false). This is useful when there's only one relevant sort option, such as a board that only supports manual ordering. Instead of hiding the control, keep it visible but disabled so users can see how the list is sorted. While disabled, neither sort-by-change nor sort-direction-change is emitted, and the direction toggle's accessible name always changes to Sorting is unavailable for <sort field>, taking precedence over sortDirectionToolTip.

A complete implementation example might look like:

With disabled sort direction
Loading live example...
import { GlSorting } from '@gitlab/ui';

Props

Name
Description
Default

text

string Text to place in the toggle button.

''

sortOptions

array Sort options to display in the dropdown. Each option is `{ value, text }`. Set `directionToggleDisabled: true` on an option that has no ascending/descending meaning (e.g. "Most relevant") to disable the sort direction toggle whenever that option is selected.

[]

sortBy

string|number The value of the item currently selected in the dropdown. Only to be used with the `sortOptions` prop.

null

isAscending

boolean Determines the current sort order icon displayed.

false

sortDirectionToolTip

string The text for the tooltip and aria-label of the sort direction toggle button instead of the defaults for ascending/descending.

null

dropdownClass

string Additional class(es) to apply to the root element of the GlCollapsibleListbox.

''

dropdownToggleClass

string Additional class(es) to apply to the dropdown toggle.

''

sortDirectionToggleClass

string Additional class(es) to apply to the sort direction toggle button.

''

block

boolean Render the dropdown toggle button as a block element

true

disabled

boolean Disables the entire sorting control (both the sort-by dropdown and the sort direction toggle) while keeping it visible.

false

size

string The size of the dropdown toggle and the sort direction toggle button.

'medium'

Events

Name
Description
sort-direction-change

undefined Emitted when the sort direction button is clicked. The event's payload will be true if the direction has been changed to ascending, or false if descending.

sort-by-change

undefined Emitted when the sort field is changed. The event's payload is the value of the selected sort field. Only emitted when using the `sortOptions` prop.

Last updated at: