npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

exsorted-react

v1.0.4

Published

TypeScript-first React custom hooks for list sorting with Exsorted (useSortedList, singleKeyAccessors)

Downloads

254

Readme

exsorted-react

TypeScript-first React sorting hook powered by exsorted.

npm version npm downloads license bundle size

  • React support: 18 and 19
  • Public API: useSortedList, singleKeyAccessors, and related public types
  • Exsorted resolution: uses peer version when available, otherwise falls back to bundled dependency

Why exsorted-react

exsorted-react provides TypeScript-first React custom hooks for sorting lists with deterministic, non-mutating behavior. It is built for teams that want reusable sorting primitives instead of rewriting sort state and comparator logic per component.

Features

  • React sorting hook API for list sorting in UI state (useSortedList)
  • Helper for single-field sorting with required accessor safety (singleKeyAccessors)
  • Supports standalone, controlled, and read-only state modes
  • Works with compare-based Exsorted algorithms and custom comparators
  • Typed return controls for sort key, direction, reset, and view restore

Use Cases

  • Build sortable tables and product lists in React apps
  • Share sorting behavior across design systems and component libraries
  • Keep sorting logic strongly typed in TypeScript codebases
  • Integrate Exsorted algorithms while preserving hook ergonomics

API Surface

import { singleKeyAccessors, useSortedList } from "exsorted-react";

singleKeyAccessors

Minimal helper for one-field sorting while keeping accessors required.

const sorted = useSortedList(products, {
  accessors: singleKeyAccessors("price", (item: Product) => item.price),
  initialKey: "price",
});

useSortedList

const result = useSortedList(items, options);

Next.js SSR / RSC

useSortedList is a client hook. In Next.js App Router, call it only in Client Components.

"use client";

import { useSortedList } from "exsorted-react";

singleKeyAccessors is a pure helper and can be called in server code, but it does not make useSortedList callable in Server Components.

If external read-only state provides a key that is not present in accessors, the hook safely falls back to the first accessor key to avoid error-triggered re-render loops.

Modes

  • Standalone mode: pass accessors with optional initialKey and initialDirection
  • Controlled mode: pass sort controller object
  • Read-only state mode: pass state object

Options

  • accessors (required): map of key to value accessor
  • comparator (optional): comparator used for sorting
  • sorter (optional): custom compare-based exsorted sorter, default is mergeSort
  • initialKey and initialDirection (standalone mode only)
  • sort (controlled mode only)
  • state (read-only state mode only)

Sorter Compatibility

sorter must match compare-based signature:

(arr, compareFn?) => arr;

Not supported because function interfaces differ:

  • exsorted/non-compare: countingSort, radixSort, bucketSort, pigeonholeSort
  • exsorted/standard: introSort

Return Value

  • items: visible items (sorted or original depending on mode/actions)
  • previousItems: previous visible snapshot
  • originalItems: input source reference
  • isSorting: transition pending state
  • isSorted: true when sorted view is active and not pending
  • sort, sortKey, direction: effective sort state
  • setSortKey, setDirection, toggleDirection, setSort, reset: sort controls
  • restoreOriginal, restoreSorted: view-mode controls

Examples

Standalone Mode

import { useSortedList } from "exsorted-react";
import { quickSort } from "exsorted";

type Product = {
  id: string;
  name: string;
  price: number;
  rating: number;
};

const sorted = useSortedList(products, {
  accessors: {
    name: (item: Product) => item.name,
    price: (item: Product) => item.price,
    rating: (item: Product) => item.rating,
  },
  sorter: quickSort,
  initialKey: "price",
});

sorted.setSortKey("name");
sorted.toggleDirection();

Single-key Minimal Mode

import { singleKeyAccessors, useSortedList } from "exsorted-react";

const sorted = useSortedList(products, {
  accessors: singleKeyAccessors("price", (item: Product) => item.price),
  initialKey: "price",
});

Controlled Mode

import { useState } from "react";
import { useSortedList } from "exsorted-react";

const [sort, setSort] = useState({ key: "name" as const, direction: "asc" as const });

const sorted = useSortedList(products, {
  accessors: {
    name: (item: Product) => item.name,
    price: (item: Product) => item.price,
  },
  sort: {
    sort,
    setSort,
    reset: () => setSort({ key: "name", direction: "asc" }),
  },
});

Read-only State Mode

const sorted = useSortedList(products, {
  accessors: {
    name: (item: Product) => item.name,
    price: (item: Product) => item.price,
  },
  state: { key: "price", direction: "desc" },
});

Custom Comparator

const sorted = useSortedList(products, {
  accessors: {
    name: (item: Product) => item.name,
    price: (item: Product) => item.price,
  },
  initialKey: "name",
  comparator: (a, b) => a.name.localeCompare(b.name, "en", { sensitivity: "base" }),
});

Tree-shaking note: import and pass only the sorter algorithm you need.