Generic type-safe array sorting utility functions for TypeScript projects
const sortedItems = sortAscending(['è', 'c', 'é', 'e', 'a', 'd', 'b']);
// ['a', 'b', 'c', 'd', 'e', 'é', 'è']of([1, 10, 5]).pipe(map(sortAscending)).subscribe();
// [1, 5, 10]const sortedItems = sortDescending(['è', 'c', 'é', 'e', 'a', 'd', 'b']);
// ['è', 'é', 'e', 'd', 'c', 'b', 'a']of([false, true, false, true]).pipe(map(sortDescending)).subscribe();
// [true, true, false, false]const items: Item[] = [
{ id: 2, title: 'a' },
{ id: 1, title: 'b' },
];
const sortedItems = sortAscendingBy('id')(items);
// [{ id: 1, title: 'b' }, { id: 2, title: 'a' }]of([
{ id: 2, title: 'a' },
{ id: 1, title: 'b' },
])
.pipe(map(sortAscendingBy('title')))
.subscribe();
// [{ id: 2, title: 'a' }, { id: 1, title: 'b' }]const items: Item[] = [
{ id: 2, title: 'a' },
{ id: 1, title: 'b' },
];
const sortedItems = sortDescendingBy('id')(items);
// [{ id: 2, title: 'a' }, { id: 1, title: 'b' }]of([
{ id: 2, title: 'a' },
{ id: 1, title: 'b' },
])
.pipe(map(sortDescendingBy('title')))
.subscribe();
// [{ id: 1, title: 'b' }, { id: 2, title: 'a' }]