Skip to content

Sorting

Enables sorting for a list element using a linked <select> element via useSorting(listEl).

Activate

Sorting is activated by adding the sortby attribute on the list element.

html
<select id="sortSelect">
  <option value="default">Default</option>
  <option value="asc">A→Z</option>
  <option value="desc">Z→A</option>
</select>

<ul filter="sortFilter" sortby="sortSelect">
  <li>Apple</li>
  <li>Banana</li>
  <li>Cherry</li>
</ul>

Method

useFilter(listEl)

ParameterTypeDescription
listElHTMLElementThe container element whose children will be sorted.

Modes

NameDescription
defaultSorts the list back in its original order.
ascSorts text in ascending alphabetical order (A → Z).
descSorts text in descending alphabetical order (Z → A).
num-ascSorts numbers in ascending order (smallest → largest).
num-descSorts numbers in descending order (largest → smallest).
date-ascSorts dates in ascending order (oldest → newest).
date-descSorts dates in descending order (newest → oldest).
len-ascSorts text by length in ascending order (shortest → longest).
len-descSorts text by length in descending order (longest → shortest).
shuffleRandomizes the order of the list.

Released under the MIT License.