Skip to main content
Login

Sign in to access your account

Sort - National Design System

Sort reorders items that are already on the page, such as cards, list rows or table rows

Overview

Sort reorders the items of a list when the user picks a trigger. Each trigger names its list with data-sort-target, and each item carries its sort values in data-sort-{key} attributes. Sort has two modes:

  • Direct mode: each trigger sets one key and one order. Use it for the options of a dropmenu.
  • Cycle mode: one trigger steps through ascending, descending and the original order. Use it for one button per key.

Filter and Tables start Sort for their own sort buttons.

Pick another component when:

  • the items are table rows with sortable columns: Tables.
  • the items are also filtered or searched: Filter.

Markup

The triggers sit in a Toolbar above the list. The list needs only the id that the triggers name.

Preview
  • Zakat Payment
    75
  • Passport Renewal
    300
  • Birth Certificate
    25
  • Identity Verification
    Free
  • Driver License
    150
  • Business Registration
    1200
<div class="nds-block"> <div class="nds-toolbar"> <div class="nds-toolbar-end"> <div class="nds-dropmenu"> <button type="button" class="nds-btn nds-secondary-outline nds-menu-btn nds-dropmenu-trigger"> <i class="nds-icon nds-hgi-sorting-05" aria-hidden="true"></i> <span class="nds-label" data-i18n="sort.sort">Sort</span> </button> <div class="nds-dropmenu-menu" hidden> <div class="nds-dropmenu-scroll"> <button type="button" class="nds-btn nds-subtle nds-dropmenu-item" data-sort-target="sort-services" data-sort> <i class="nds-icon nds-hgi-sorting-05" aria-hidden="true"></i> <span class="nds-label">Default order</span> </button> <button type="button" class="nds-btn nds-subtle nds-dropmenu-item" data-sort-target="sort-services" data-sort="name" data-sort-order="asc"> <i class="nds-icon nds-hgi-sort-by-up-02" aria-hidden="true"></i> <span class="nds-label">Name A to Z</span> </button> <button type="button" class="nds-btn nds-subtle nds-dropmenu-item" data-sort-target="sort-services" data-sort="name" data-sort-order="desc"> <i class="nds-icon nds-hgi-sort-by-down-02" aria-hidden="true"></i> <span class="nds-label">Name Z to A</span> </button> <button type="button" class="nds-btn nds-subtle nds-dropmenu-item" data-sort-target="sort-services" data-sort="price" data-sort-order="asc"> <i class="nds-icon nds-hgi-sort-by-up-02" aria-hidden="true"></i> <span class="nds-label">Price low to high</span> </button> <button type="button" class="nds-btn nds-subtle nds-dropmenu-item" data-sort-target="sort-services" data-sort="price" data-sort-order="desc"> <i class="nds-icon nds-hgi-sort-by-down-02" aria-hidden="true"></i> <span class="nds-label">Price high to low</span> </button> </div> </div> </div> </div> </div> <ul id="sort-services" class="nds-list nds-grid nds-stroke" style="--max-col:3;--mid-col:2;--min-col:2;"> <li class="nds-card" data-sort-name="Zakat Payment" data-sort-price="75"> <div class="nds-card-content"> <div class="nds-card-text"> <span class="nds-card-title">Zakat Payment</span> </div> <div class="nds-card-value"> <span class="nds-number-format" data-currency="SAR">75</span> </div> </div> </li> <li class="nds-card" data-sort-name="Passport Renewal" data-sort-price="300"> <div class="nds-card-content"> <div class="nds-card-text"> <span class="nds-card-title">Passport Renewal</span> </div> <div class="nds-card-value"> <span class="nds-number-format" data-currency="SAR">300</span> </div> </div> </li> <li class="nds-card" data-sort-name="Birth Certificate" data-sort-price="25"> <div class="nds-card-content"> <div class="nds-card-text"> <span class="nds-card-title">Birth Certificate</span> </div> <div class="nds-card-value"> <span class="nds-number-format" data-currency="SAR">25</span> </div> </div> </li> <li class="nds-card" data-sort-name="Identity Verification" data-sort-price="0"> <div class="nds-card-content"> <div class="nds-card-text"> <span class="nds-card-title">Identity Verification</span> </div> <div class="nds-card-value"> <span class="nds-number-format">Free</span> </div> </div> </li> <li class="nds-card" data-sort-name="Driver License" data-sort-price="150"> <div class="nds-card-content"> <div class="nds-card-text"> <span class="nds-card-title">Driver License</span> </div> <div class="nds-card-value"> <span class="nds-number-format" data-currency="SAR">150</span> </div> </div> </li> <li class="nds-card" data-sort-name="Business Registration" data-sort-price="1200"> <div class="nds-card-content"> <div class="nds-card-text"> <span class="nds-card-title">Business Registration</span> </div> <div class="nds-card-value"> <span class="nds-number-format" data-currency="SAR">1200</span> </div> </div> </li> </ul> </div>

Behavior

Markup Wiring

The script wires every list that a data-sort-target names, after the page first paints. A trigger can sit anywhere on the page, in a dropmenu too. Every child of the list is an item, so the list holds only the items. For a custom value reader, fixed types, URL sync or a change callback, call create() instead (see the API). Leave data-sort-target off those triggers: a list keeps its first instance, so a second setup is ignored. On a list that Pagination pages, a new sort opens page 1.

Direct Mode

Each trigger fixes its own key and order, so every choice has a label. A trigger with an empty data-sort restores the authored order. Set data-sort-order to asc or desc. Without it the trigger sorts ascending and never toggles.

Cycle Mode

One trigger holds the state for one key: the first click sorts ascending, the second descending, the third restores the authored order. A click on another trigger starts that key at ascending. Write data-sort-mode="cycle" on the triggers, or set mode: 'cycle' in create().

Initial State

When the server sends the items already sorted, write data-state="sorted-asc" or "sorted-desc" on the trigger of that order. In a table, write it on the header cell (th) that holds the trigger, never on the button: Sort moves it from that cell on the next sort. Sort records the state without moving the items, and the next click goes on from that state. In create(), initialState: { key, order } does the same, and wins over the markup. A sort in the URL (urlSync) wins over both: Sort applies it and moves the state to its trigger, so a shared link opens in its own order.

URL Persistence

In create(), set urlSync: { keyParam, orderParam } and the sort key and order go in the query string: urlSync: { keyParam: 'sort', orderParam: 'order' } writes ?sort=price&order=desc. Ascending writes no order, and other parameters stay. The URL counts only when its key is one of the triggers’ keys, so another use of the same parameter, such as a server’s own sort select, is left alone. Sort removes only values it could have written: a site’s own ?order=12345 stays. Sort reads them on create() and writes them on every change. Use it on list pages where users share links or reload. Markup wiring leaves the URL alone: a Filter keeps the sort of its own buttons in the URL, as sort and order.

Built-in Features

  • Used by Filter and Tables

    Filter starts Sort for its [data-sort] buttons. Tables starts it for its sortable column headers. Neither needs a data-sort-target.

  • Type Detection

    Sort reads the values at sort time and picks number, date or text. A value such as 9,375 SAR sorts as a number. A date in the list's date format sorts as a date: the nearest data-date-format around the list, such as on <html>, or DD/MM/YYYY, in Latin or Arabic digits. It reads in the nearest data-calendar, so a Hijri column sorts by its Hijri dates. So do YYYY-MM-DD and ISO dates with a time. Other text with / or : between digits sorts as text, never as a number. Text sorts in the language of the browser, and numbers inside text stay in order: item 2 comes before item 10.

  • Sort Icon

    An icon with nds-sort-icon in a trigger turns up or down with the sort, and back when the sort is cleared. No script of yours swaps it.

  • Keyboard and ARIA

    Triggers respond to Enter and Space. The active trigger carries aria-pressed="true". In create(), the a11y option writes aria-sort on a column header instead.

  • Dropmenu Icon Sync

    When the triggers sit in a dropmenu, the icon on the closed trigger button copies the icon of the chosen item.

  • Original Order

    Sort keeps the authored order and restores it on reset. It moves the existing elements, so their listeners and state stay.

  • Programmatic Control

    Call setSort(), reset() and refresh() from your own code. Listen for nds:sort:change to react to every sort.

Best Practices

  • Decide who sorts before you write a trigger. If the page holds every matching row, Sort reorders them. If the server sends one page at a time, the server must sort. Then write no data-sort triggers. In a Filter form, a Dropmenu picker sends the sort with the request.
  • Point data-sort-target only at a list that no Filter or table sorts. Those sort their own list, and NDS.init.audit() warns about the button. For a Filter’s list, write the filter’s data-filter-target on each trigger in its place.
  • Write direct mode triggers as an ascending and a descending pair, and add a trigger with an empty data-sort to restore the original order.
  • Use cycle mode when one control carries one key. Users expect the third click to undo the sort. Write data-sort-mode="cycle" on every trigger of the list.
  • Give each trigger a text label, or an aria-label when it shows only an icon.
  • Keep display text and the sort value apart when they differ. A card that reads “Free” carries data-sort-price="0", so it sorts as the cheapest.
  • Write a date in its sort attribute as YYYY-MM-DD, such as data-sort-added="2026-03-15". The text can show any format or calendar.
  • Set types: { key: 'string' } in create() for codes that start with a number, such as 10-B. Detection reads them as the number 10, so 10-A and 10-B tie and keep their order.
  • In create(), put the triggers and the items under the root: a selector string resolves inside it. Pass a function for elements outside it.
  • Keep the accessor fast and free of side effects: Sort calls it on every comparison.
  • Call NDS.init.refresh(list) after you add items, such as after a request. New items join the active sort only after that call. On your own instance, call refresh().
  • Use onChange in the code that creates the instance. Use the nds:sort:change event in other code.
  • On a list that Pagination pages, call NDS.Pagination.refresh(list) in the onChange of your own create(). Pagination does not see a sort move the items.
  • Call NDS.init.destroy(view) before you remove a view in a single-page app. It removes every listener Sort added.

API

Sort ships in the delegated bundle, which the loader injects after first paint. A click before the bundle arrives does nothing, and the next click works. A create() call before the bundle arrives still works, but returns a Promise instead of the instance. To get the instance at once, await NDS.loadBundle('delegated') first.

Classes

Class Element Effect
nds-sort-icon the icon in a trigger Shows the up icon while its trigger, or the table header of its trigger, carries sorted-asc, and the down icon with sorted-desc. Otherwise it shows the icon class it has.

Data Attributes

Attribute Element Effect
data-sort-target a trigger The id of the list it sorts. The script wires each list it names.
data-sort a trigger The sort key. Empty: restore the original order.
data-sort-order a direct mode trigger asc or desc. Missing means asc.
data-sort-mode a trigger with data-sort-target cycle puts every trigger of that list in cycle mode. Missing means direct mode.
data-state~="sorted-asc", "sorted-desc" the active trigger, or its table header with a11y: 'sort' The script sets it after each sort, and removes it when another sort runs or the sort is cleared. Write it at load when the items arrive in that order: Sort records the state and does not move them.
data-state~="selected" the active trigger With a11y: 'pressed', the default. The script sets it with aria-pressed="true" after each sort. When another trigger is active or the sort is cleared, it removes it and sets aria-pressed="false".
data-state~="active" the active trigger With a11y: 'sort'. The script sets it after each sort, and removes it when another trigger is active or the sort is cleared.
aria-sort the table header of a trigger With a11y: 'sort'. The script writes ascending or descending on the active header, and none on the others.
data-sort-{key} an item The value the default accessor sorts by for that key.

JavaScript

The NDS.Sort script loads after the page shows. Until it loads, each call loads it and returns a promise of the result, so await a call whose result you use.

Markup wiring is a create() call on the list with the defaults below: items is the children of the list, triggers is the buttons that name it, and data-sort-mode sets mode. Call create() yourself for any other option.

Method Effect
NDS.Sort.init() Wires every list that a data-sort-target names, and skips a list that already has an instance. The loader calls it.
NDS.Sort.refresh(root) Wires new lists, then sorts again each list that markup wired inside root, or that holds root. An instance from your own create() call needs its refresh(). NDS.init.refresh() calls it.
NDS.Sort.create(root, options) Returns the instance of the root. A root that already has one returns it and ignores options.
NDS.Sort.destroy(root) The same as sort.destroy(). root is an element or a selector.
NDS.Sort.getInstance(root) Returns the instance of the root, or null. root is an element or a selector.
NDS.Sort.detectType(values, dateFormat, calendar) Returns 'number', 'date' or 'string'. dateFormat is the format a date in the values uses, and calendar its calendar: the site’s when you leave them out.
NDS.Sort.parseValue(raw, type, dateFormat, calendar) Returns the value as the type sorts it.
NDS.Sort.compare(a, b, type, order, dateFormat, calendar) Returns a negative, zero or positive number.
sort.setSort(key, order) Sorts now. A null key restores the original order.
sort.reset() Restores the original order. Items added after setup follow the others.
sort.refresh() Sorts the items again by the active key. Does nothing when no sort is active.
sort.getState() Returns { key, order }.
sort.hasMarkupTriggers A property. true when data-sort-target buttons drive the sort, not a Filter or a Table.
sort.destroy() Removes the listeners. The sort states on the triggers and the order of the items stay.

The create() options:

Option Default Effect
items required The elements to move. A selector, a NodeList, an array or a function that returns them. A function is called on every sort.
triggers required The elements that start a sort. Same forms as items. Triggers inside a dropmenu menu are found.
reorderIn the parent of the first item The element the items are appended into.
mode 'direct' 'direct' or 'cycle'.
a11y 'pressed' 'pressed', 'sort' or 'none'. The Data Attributes table lists what each one writes.
a11yTarget the closest th With a11y: 'sort', a function from a trigger to the element that carries aria-sort and the sorted-asc or sorted-desc state.
accessor reads data-sort-{key} A function (item, key) => value that returns the raw value.
keyFrom reads data-sort A function from a trigger to its key. An empty key restores the original order.
types {} The type of a key: 'number', 'date' or 'string'. Replaces detection for that key.
initialState null { key, order }. Records the state without moving items. Wins over a sorted-asc or sorted-desc state in the markup.
urlSync false { keyParam, orderParam }. Reads and writes the query string.
onChange none A function ({ key, order, orderedItems, state }), called after every sort and before the event.
Event Fired on Detail
nds:sort:change the root, bubbles { key, order, orderedItems, sort }, after every sort, reset and apply(). sort is the root; its instance is sort.ndsSort.
const sort = NDS.Sort.create(document.getElementById('list'), { items: '.row', triggers: '[data-sort]', types: { added: 'date' }, initialState: { key: 'added', order: 'desc' }, urlSync: { keyParam: 'sort', orderParam: 'order' } }); sort.setSort('price', 'desc'); sort.reset(); document.addEventListener('nds:sort:change', (e) => { const { key, order, orderedItems } = e.detail; });

The full API is in the banner of _js/nds-sort.js.

Last Modified Date: 02/11/2026 - 10:54 PM
Was this page useful?
60% of users said Yes from 2843 Feedbacks