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:
Markup
The triggers sit in a Toolbar above the list. The list needs only the id that the triggers name.
-
Zakat Payment75
-
Passport Renewal300
-
Birth Certificate25
-
Identity VerificationFree
-
Driver License150
-
Business Registration1200
Variants
| Group | Option | Markup | On element | Use |
|---|---|---|---|---|
| Structure | Direct (default) (hint: Each menu item sets one key and one order) | — | — | A dropmenu of sort choices in a Toolbar. Each item fixes one key and one order |
| Structure | Cycle (hint: Each click steps through up, down and the original order) | canon #sort-cycle |
— | One button per key, with data-sort-mode="cycle". Each click steps its key through ascending, descending and the original order, and its .nds-sort-icon turns to match |
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
-
-
Type Detection
Sort reads the values at sort time and picks number, date or text. A value such as
9,375 SARsorts as a number. A date in the list's date format sorts as a date: the nearestdata-date-formataround the list, such as on<html>, orDD/MM/YYYY, in Latin or Arabic digits. It reads in the nearestdata-calendar, so a Hijri column sorts by its Hijri dates. So doYYYY-MM-DDand 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-iconin 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". Increate(), thea11yoption writesaria-sorton 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()andrefresh()from your own code. Listen fornds:sort:changeto 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-sorttriggers. In a Filter form, a Dropmenu picker sends the sort with the request. - Point
data-sort-targetonly at a list that no Filter or table sorts. Those sort their own list, andNDS.init.audit()warns about the button. For a Filter’s list, write the filter’sdata-filter-targeton each trigger in its place. - Write direct mode triggers as an ascending and a descending pair, and add a trigger with an empty
data-sortto 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-labelwhen 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 asdata-sort-added="2026-03-15". The text can show any format or calendar. - Set
types: { key: 'string' }increate()for codes that start with a number, such as10-B. Detection reads them as the number 10, so10-Aand10-Btie 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
accessorfast 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, callrefresh(). - Use
onChangein the code that creates the instance. Use thends:sort:changeevent in other code. - On a list that Pagination pages, call
NDS.Pagination.refresh(list)in theonChangeof your owncreate(). 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. |
The full API is in the banner of _js/nds-sort.js.