Function sortBy
- sort
By <T>(array, selector, options?): T[] Type Parameters
Parameters
- array: readonly T[]
The array to sort.
- selector: ((el: T) => number)
The selector function to get the value to sort by.
- (el): number
Parameters
- el: T
Returns number
Optionaloptions: SortByOptionsThe options for sorting.
Returns T[]
A new array containing all elements sorted by the selector.
Example: Usage
import { sortBy } from "@std/collections/sort-by";
import { assertEquals } from "@std/assert";
const people = [
{ name: "Anna", age: 34 },
{ name: "Kim", age: 42 },
{ name: "John", age: 23 },
];
const sortedByAge = sortBy(people, (person) => person.age);
assertEquals(sortedByAge, [
{ name: "John", age: 23 },
{ name: "Anna", age: 34 },
{ name: "Kim", age: 42 },
]);
const sortedByAgeDesc = sortBy(people, (person) => person.age, { order: "desc" });
assertEquals(sortedByAgeDesc, [
{ name: "Kim", age: 42 },
{ name: "Anna", age: 34 },
{ name: "John", age: 23 },
]);- array: readonly T[]
- sort
By <T>(array, selector, options?): 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 through the
orderoption. By default, the elements are sorted in ascending order.Type Parameters
Parameters
- array: readonly T[]
The array to sort.
- selector: ((el: T) => string)
The selector function to get the value to sort by.
- (el): string
Parameters
- el: T
Returns string
Optionaloptions: SortByOptionsThe options for sorting.
Returns T[]
A new array containing all elements sorted by the selector.
Example: Usage
import { sortBy } from "@std/collections/sort-by";
import { assertEquals } from "@std/assert";
const people = [
{ name: "Anna" },
{ name: "Kim" },
{ name: "John" },
];
const sortedByName = sortBy(people, (it) => it.name);
assertEquals(sortedByName, [
{ name: "Anna" },
{ name: "John" },
{ name: "Kim" },
]);- array: readonly T[]
- sort
By <T>(array, selector, options?): 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 through the
orderoption. By default, the elements are sorted in ascending order.Type Parameters
Parameters
- array: readonly T[]
The array to sort.
- selector: ((el: T) => bigint)
The selector function to get the value to sort by.
- (el): bigint
Parameters
- el: T
Returns bigint
Optionaloptions: SortByOptionsThe options for sorting.
Returns T[]
A new array containing all elements sorted by the selector.
Example: Usage
import { sortBy } from "@std/collections/sort-by";
import { assertEquals } from "@std/assert";
const people = [
{ name: "Anna", age: 34n },
{ name: "Kim", age: 42n },
{ name: "John", age: 23n },
];
const sortedByAge = sortBy(people, (person) => person.age);
assertEquals(sortedByAge, [
{ name: "John", age: 23n },
{ name: "Anna", age: 34n },
{ name: "Kim", age: 42n },
]);- array: readonly T[]
- sort
By <T>(array, selector, options?): 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 through the
orderoption. By default, the elements are sorted in ascending order.Type Parameters
Parameters
- array: readonly T[]
The array to sort.
- selector: ((el: T) => Date)
The selector function to get the value to sort by.
- (el): Date
Parameters
- el: T
Returns Date
Optionaloptions: SortByOptionsThe options for sorting.
Returns T[]
A new array containing all elements sorted by the selector.
Example: Usage
import { sortBy } from "@std/collections/sort-by";
import { assertEquals } from "@std/assert";
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, (people) => people.startedAt);
assertEquals(sortedByStartedAt, [
{ name: "Anna", startedAt: new Date("2020-01-01") },
{ name: "Kim", startedAt: new Date("2020-03-01") },
{ name: "John", startedAt: new Date("2020-06-01") },
]);- array: readonly 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 through the
orderoption. By default, the elements are sorted in ascending order.Note: If you want to process any iterable, use the new version of
sortByfrom@std/collections/unstable-sort-by.