Projects
fluent-sorter is a small utility for composing sort comparators with a fluent thenBy API.
pnpm add fluent-sorter
import sorter from "fluent-sorter"; // either use default export sorter or thenBy directly
const someApiResponse = await fetch("https://api.example.com/items").then(res => res.json());
return someApiResponse.sort(s => s.createdAt, "desc").thenBy(s => s.name, "asc"); // sort by createdAt descending and then by name ascending
import sorter from "fluent-sorter"; // either use default export sorter or thenBy directly
const byScoreThenName = sorter<{ score: number; name: string }>((a, b) => b.score - a.score).thenBy(
(value) => value.name,
"asc"
);
byScoreThenName({ score: 10, name: "Alice" }, { score: 20, name: "Bob" }); // -1
byScoreThenName({ score: 20, name: "Bob" }, { score: 10, name: "Alice" }); // 1
byScoreThenName({ score: 10, name: "Alice" }, { score: 10, name: "Bob" }); // 0
import { thenBy } from "fluent-sorter"; // either use default export sorter or thenBy directly
const byScoreThenName = thenBy<{ score: number; name: string }>((value) => value.score, "desc").thenBy(
(value) => value.name,
"asc"
);
import { thenBy } from "fluent-sorter"; // either use default export sorter or thenBy directly
const byScoreThenName = thenBy<{ score: number; name: string }>((value) => value.score, "desc").thenBy(
(value) => value.name,
"asc"
);
const { thenBy } = require("fluent-sorter");
const byScoreThenName = thenBy<{ score: number; name: string }>((value) => value.score, "desc").thenBy(
(value) => value.name,
"asc"
);
This package is built with Bun and published with dual-module support:
dist/index.jsdist/index.cjsdist/index.d.tsbun run build