跳到主要内容

sortByKey

TS JS Deno

Sort by based on the key.

Use the spread operator (...), Array.prototype.sort() and String.localeCompare() to sort array of data.

typescript
enum SortByOrder {
ASC = 1,
DESC = -1,
}
const sortByKey = <T = any>(
arr: T[],
key: string,
order: SortByOrder = SortByOrder.ASC
) => {
return [...arr].sort(
(s1: any, s2: any) => order * String(s1[key]).localeCompare(String(s2[key]))
);
};
typescript
sortBy([{ name: "02" }, { name: "01" }], "name"); // [{ name: "01" },{ name: "02" }]
// Descending
sortBy([{ name: "02" }, { name: "01" }], "name", -1); // [{ name: "02" },{ name: "01" }]