client/src/utils/sort.ts (5 lines of code) (raw):

export const stableSort = <T>(arr: T[], compare: (a: T, b: T) => number) => arr .map((item, index) => ({ item, index })) .sort((a, b) => compare(a.item, b.item) || a.index - b.index) .map(({ item }) => item);