Equality
Equality

Sort Selector

Dropdown for choosing a list's sort field and order

View Markdown

Overview

Sort Selector is a dropdown for choosing how a list is sorted. It presents a curated set of field-and-direction options — Name (A–Z / Z–A) plus date options — and reflects the current choice on its trigger. It’s built on a Dropdown Menu with a Button trigger.

It is a controlled component: you hold the current sortField and sortOrder and update them from the setSortField/setSortOrder callbacks. When the current sort differs from the configured defaults, a Reset link appears in the menu to return to them.

Usage

Import the component:

import { SortSelector } from "@eqtylab/equality";

Hold the field and order in state, then apply them to your data:

const [sortField, setSortField] = useState("name");
const [sortOrder, setSortOrder] = useState("asc");

<SortSelector
  sortField={sortField}
  sortOrder={sortOrder}
  setSortField={setSortField}
  setSortOrder={setSortOrder}
/>;

The component only selects the sort preference — apply it to your list yourself, for example:

const sorted = [...items].sort(compareBy(sortField, sortOrder));

Default

Shows the Sort Selector with default sorting (by Name, ascending):

Sort Mode

The sortMode prop determines which date options are offered. It accepts "created" (default) or "updated":

  • sortMode="created" → “Recently Created” / “Oldest Created”
  • sortMode="updated" → “Recently Updated” / “Oldest Updated”

Hide Date Options

Set showDateOptions={false} to offer only the Name options:

Reset

When the current sort no longer matches defaultSortField/defaultSortOrder (both name/asc by default), a Reset link appears in the dropdown that restores the defaults. Open the menu and pick a different option to see it.

Props

NameDescriptionTypeDefaultRequired
sortFieldThe currently selected sort field (controlled).name, type, createdAt, updatedAt
sortOrderThe current sort direction (controlled).asc, desc
setSortFieldCalled with the new field when the selection changes.(field: SortField) => void
setSortOrderCalled with the new order when the selection changes.(order: SortOrder) => void
sortModeWhich date options are offered.created, updatedcreated
showDateOptionsWhether the date options are shown.booleantrue
defaultSortFieldThe field the Reset link restores to.name, type, createdAt, updatedAtname
defaultSortOrderThe order the Reset link restores to.asc, descasc
setCurrentPageOptional. Called with 1 on any change, to reset pagination.(page: number) => void
classNameAdditional CSS classes applied to the trigger button.string