Skip to main content
Module

std/collections/mod.ts>sortBy

Deno standard library
Go to Latest
function sortBy
import { sortBy } from "https://deno.land/std@0.106.0/collections/mod.ts";

Returns all elements in the given collection, sorted stably by their result using the given selector

Example:

import { sortBy } from "./sort_by.ts"
import { assertEquals } from "../testing/asserts.ts";

const people = [
    { name: 'Anna', age: 34 },
    { name: 'Kim', age: 42 },
    { name: 'John', age: 23 },
]
const sortedByAge = sortBy(people, it => it.age)

assertEquals(sortedByAge, [
    { name: 'John', age: 23 },
    { name: 'Anna', age: 34 },
    { name: 'Kim', age: 42 },
])

Parameters

array: readonly T[]
selector:
| ((el: T) => number)
| ((el: T) => string)
| ((el: T) => bigint)
| ((el: T) => Date)