Skip to main content

latest
Works with
It is unknown whether this package works with Browsers, Deno, Node.js, Cloudflare Workers, Bun
It is unknown whether this package works with Browsers
It is unknown whether this package works with Deno
It is unknown whether this package works with Node.js
It is unknown whether this package works with Cloudflare Workers
It is unknown whether this package works with Bun
JSR Score70%
Downloads1/wk
Published2 years ago (0.215.0)
function sortBy
sortBy<T>(
array: readonly T[],
selector: (el: T) => number,
options?: SortByOptions
): T[]

Returns all elements in the given collection, sorted by their result using the given selector. The selector function is called only once for each element. Ascending or descending order can be specified.

Examples

Example 1

import { sortBy } from "@std/collections/sort_by";
import { assertEquals } from "@std/assert/assert_equals";

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 },
]);

const sortedByAgeDesc = sortBy(people, (it) => it.age, { order: "desc" });

assertEquals(sortedByAgeDesc, [
  { name: "Kim", age: 42 },
  { name: "Anna", age: 34 },
  { name: "John", age: 23 },
]);

Type Parameters

Parameters

array: readonly T[]
selector: (el: T) => number
optional
options: SortByOptions

Return Type

sortBy<T>(
array: readonly T[],
selector: (el: T) => string,
options?: SortByOptions
): T[]

Returns all elements in the given collection, sorted by their result using the given selector. The selector function is called only once for each element. Ascending or descending order can be specified.

Examples

Example 1

import { sortBy } from "@std/collections/sort_by";

const people = [
  { name: "Anna" },
  { name: "Kim" },
  { name: "John" },
];
const sortedByName = sortBy(people, (it) => it.name);

Type Parameters

Parameters

array: readonly T[]
selector: (el: T) => string
optional
options: SortByOptions

Return Type

sortBy<T>(
array: readonly T[],
selector: (el: T) => bigint,
options?: SortByOptions
): T[]

Returns all elements in the given collection, sorted by their result using the given selector. The selector function is called only once for each element. Ascending or descending order can be specified.

Examples

Example 1

import { sortBy } from "@std/collections/sort_by";

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

Type Parameters

Parameters

array: readonly T[]
selector: (el: T) => bigint
optional
options: SortByOptions

Return Type

sortBy<T>(
array: readonly T[],
selector: (el: T) => Date,
options?: SortByOptions
): T[]

Returns all elements in the given collection, sorted by their result using the given selector. The selector function is called only once for each element. Ascending or descending order can be specified.

Examples

Example 1

import { sortBy } from "@std/collections/sort_by";

const people = [
  { name: "Anna", startedAt: new Date("2020-01-01") },
  { name: "Kim", startedAt: new Date("2020-03-01") },
  { name: "John", startedAt: new Date("2020-06-01") },
];
const sortedByStartedAt = sortBy(people, (it) => it.startedAt);

Type Parameters

Parameters

array: readonly T[]
selector: (el: T) => Date
optional
options: SortByOptions

Return Type

Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@std/collections

Import symbol

import { sortBy } from "@std/collections";
or

Import directly with a jsr specifier

import { sortBy } from "jsr:@std/collections";