Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

x/ayonli_jsext/array/index.ts>orderBy

A JavaScript extension package for building strong and modern applications.
Latest
function orderBy
import { orderBy } from "https://deno.land/x/ayonli_jsext@v0.9.72/array/index.ts";

Orders the items of the array according to the given callback function.

Examples

Example 1

import { orderBy } from "@ayonli/jsext/array";

const arr = [
    { id: 1, name: "foo" },
    { id: 2, name: "bar" },
    { id: 3, name: "baz" },
    { id: 4, name: "qux" },
];

console.log(orderBy(arr, item => item.name));
// [
//     { id: 2, name: "bar" },
//     { id: 3, name: "baz" },
//     { id: 1, name: "foo" },
//     { id: 4, name: "qux" }
// ]

console.log(orderBy(arr, item => item.id, "desc"));
// [
//     { id: 4, name: "qux" },
//     { id: 3, name: "baz" },
//     { id: 2, name: "bar" },
//     { id: 1, name: "foo" }
// ]

Parameters

arr: T[]
fn: (item: T, i: number) => string | number | bigint
optional
order: "asc" | "desc"

Orders the items of the array according to the specified comparable key (whose value must either be a numeric or string).

Parameters

arr: T[]
key: keyof T
optional
order: "asc" | "desc"