Vize

use-sorted-locale

Sort strings locale-aware with Intl.Collator.

Package @vizejs/composable/use-sorted-locale
Own the source vize lib pull composable:use-sorted-locale
Runtime exports useSortedLocale
Gzip budget 2304 B

Usage

import { useSortedLocale } from "@vizejs/composable/use-sorted-locale";

Runtime contract

Utility Category Stability SSR Hydration Cleanup Targets Host globals Uses
useSortedLocale i18n experimental deterministic-fallback caller-managed none web, server, worker, native, desktop, terminal Intl, window useLocale

API

useSortedLocale

Sort strings locale-aware with Intl.Collator.

function useSortedLocale( items: MaybeRefOrGetter<readonly string[]>, options?: MaybeRefOrGetter<UseSortedLocaleOptions<string>>, ): SortedLocaleControls<string>

useSortedLocale

Sort any items locale-aware by a string key with Intl.Collator.

function useSortedLocale<Item>( items: MaybeRefOrGetter<readonly Item[]>, options: MaybeRefOrGetter< UseSortedLocaleOptions<Item> & { readonly key: (item: Item) => string } >, ): SortedLocaleControls<Item>

useSortedLocale

Sort items with locale-aware collation (Intl.Collator): "รค" sorts next to "a" in German but after "z" in Swedish, and numeric: true orders "item 10" after "item 9". The result is a new, stable-sorted array recomputed when the items, options, or locale change; the source array is never mutated. Invalid options surface as the platform RangeError. Derived state only: safe and deterministic on the server when the locale is explicit.

function useSortedLocale<Item>( items: MaybeRefOrGetter<readonly Item[]>, options: MaybeRefOrGetter<UseSortedLocaleOptions<Item>> = {}, ): SortedLocaleControls<Item>
const { sorted } = useSortedLocale(users, { key: (user) => user.name, locale: "de", sensitivity: "base" });

Types

UseSortedLocaleOptions

Options for useSortedLocale.

Member Type Description
locale? string | Intl.Locale Locale whose collation order is used. Pass it explicitly for hydration-stable server rendering.
descending? boolean Sort from Z to A.
key? (item: Item) => string Text to compare for each item. Required for non-string items.

SortedLocaleControls

Reactive locale-aware sorting returned by useSortedLocale.

Member Type Description
locale ComputedRef<string> Canonical locale in use.
collator ComputedRef<Intl.Collator> Collator for the current locale and options.
sorted ComputedRef<readonly Item[]> New array sorted by locale collation; the source is never mutated.
compare (left: string, right: string) => number Compare two strings with the current collator (ignores descending).