Open this example in the interactive library
Source code
type Row = { id: number; name: string; rank: number | null };
const rows: Row[] = [
{ id: 2, name: "Beta", rank: null },
{ id: 1, name: "Alpha", rank: 3 },
];
console.log(
(<T,>(values: T[], select: (v: T) => string) =>
values.toSorted((a, b) => select(a).localeCompare(select(b))))(
rows,
(r) => r.name,
),
);
Sample output
[
{ id: 1, name: 'Alpha', rank: 3 },
{ id: 2, name: 'Beta', rank: null }
]