naturalCompare

Comparing values the way a reader expects, for sorting tables, navs and lists.

import { naturalCompare } from 'tosijs-ui'

const rows = ['9', '399', '1200', '3.5', '40']
preview.textContent = rows.slice().sort(naturalCompare).join(', ')
// 3.5, 9, 40, 399, 1200   — not 1200, 3.5, 399, 40, 9

A raw > compares strings lexically, so any column of numeric strings sorts by first digit — and real data is full of them: CSV and TSV imports, BigQuery exports, JSON where numbers arrived as strings, anything id-shaped. '9' > '399' is true.

naturalCompare handles three things a naive comparator gets wrong:

Blank values (null, undefined, '') sort last in both directions — a descending sort that opens on a screenful of empty cells is never what was clicked for.