Skip to main content
Module

std/collections/sort_by.ts

Deno standard library
Go to Latest
File
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
/** * Returns all elements in the given collection, sorted stably by their result using the given selector * * Example: * * ```ts * 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 }, * ]) * ``` */export function sortBy<T>( array: readonly T[], selector: | ((el: T) => number) | ((el: T) => string) | ((el: T) => bigint) | ((el: T) => Date),): T[] { return Array.from(array).sort((a, b) => { const selectedA = selector(a); const selectedB = selector(b);
if (typeof selectedA === "number") { if (Number.isNaN(selectedA)) { return 1; }
if (Number.isNaN(selectedB)) { return -1; } }
if (selectedA > selectedB) { return 1; }
if (selectedA < selectedB) { return -1; }
return 0; });}