2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2021-09-18 18:29:29 +00:00
|
|
|
// This module is browser compatible.
|
2021-08-03 15:32:47 +00:00
|
|
|
|
2023-12-05 08:53:55 +00:00
|
|
|
/** Order option for {@linkcode SortByOptions}. */
|
2023-06-07 05:09:28 +00:00
|
|
|
export type Order = "asc" | "desc";
|
|
|
|
|
2023-12-05 08:53:55 +00:00
|
|
|
/** Options for {@linkcode sortBy}. */
|
2023-06-07 05:09:28 +00:00
|
|
|
export type SortByOptions = {
|
2024-05-06 07:51:20 +00:00
|
|
|
/**
|
|
|
|
* The order to sort the elements in.
|
|
|
|
*
|
|
|
|
* @default {"asc"}
|
|
|
|
*/
|
2023-06-07 05:09:28 +00:00
|
|
|
order: Order;
|
|
|
|
};
|
|
|
|
|
2021-08-03 15:32:47 +00:00
|
|
|
/**
|
2022-11-25 11:40:23 +00:00
|
|
|
* Returns all elements in the given collection, sorted by their result using
|
|
|
|
* the given selector. The selector function is called only once for each
|
2024-05-06 07:51:20 +00:00
|
|
|
* element. Ascending or descending order can be specified through the `order`
|
|
|
|
* option. By default, the elements are sorted in ascending order.
|
2021-08-03 15:32:47 +00:00
|
|
|
*
|
2024-05-20 07:34:47 +00:00
|
|
|
* @typeParam T The type of the array elements.
|
2024-05-06 07:51:20 +00:00
|
|
|
*
|
|
|
|
* @param array The array to sort.
|
|
|
|
* @param selector The selector function to get the value to sort by.
|
|
|
|
* @param options The options for sorting.
|
|
|
|
*
|
|
|
|
* @returns A new array containing all elements sorted by the selector.
|
|
|
|
*
|
|
|
|
* @example Usage
|
2021-08-03 15:32:47 +00:00
|
|
|
* ```ts
|
2024-04-29 02:57:30 +00:00
|
|
|
* import { sortBy } from "@std/collections/sort-by";
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assertEquals } from "@std/assert";
|
2021-08-03 15:32:47 +00:00
|
|
|
*
|
|
|
|
* const people = [
|
2022-11-25 11:40:23 +00:00
|
|
|
* { name: "Anna", age: 34 },
|
|
|
|
* { name: "Kim", age: 42 },
|
|
|
|
* { name: "John", age: 23 },
|
|
|
|
* ];
|
2024-05-06 07:51:20 +00:00
|
|
|
* const sortedByAge = sortBy(people, (person) => person.age);
|
2021-08-03 15:32:47 +00:00
|
|
|
*
|
2021-08-10 08:46:14 +00:00
|
|
|
* assertEquals(sortedByAge, [
|
2022-11-25 11:40:23 +00:00
|
|
|
* { name: "John", age: 23 },
|
|
|
|
* { name: "Anna", age: 34 },
|
|
|
|
* { name: "Kim", age: 42 },
|
|
|
|
* ]);
|
2023-06-07 05:09:28 +00:00
|
|
|
*
|
2024-05-06 07:51:20 +00:00
|
|
|
* const sortedByAgeDesc = sortBy(people, (person) => person.age, { order: "desc" });
|
2023-06-07 05:09:28 +00:00
|
|
|
*
|
|
|
|
* assertEquals(sortedByAgeDesc, [
|
|
|
|
* { name: "Kim", age: 42 },
|
|
|
|
* { name: "Anna", age: 34 },
|
|
|
|
* { name: "John", age: 23 },
|
|
|
|
* ]);
|
2021-08-03 15:32:47 +00:00
|
|
|
* ```
|
|
|
|
*/
|
2021-09-21 06:05:51 +00:00
|
|
|
export function sortBy<T>(
|
|
|
|
array: readonly T[],
|
2023-11-21 12:37:43 +00:00
|
|
|
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
|
2024-05-06 07:51:20 +00:00
|
|
|
* element. Ascending or descending order can be specified through the `order`
|
|
|
|
* option. By default, the elements are sorted in ascending order.
|
|
|
|
*
|
2024-05-20 07:34:47 +00:00
|
|
|
* @typeParam T The type of the array elements.
|
2023-11-21 12:37:43 +00:00
|
|
|
*
|
2024-05-06 07:51:20 +00:00
|
|
|
* @param array The array to sort.
|
|
|
|
* @param selector The selector function to get the value to sort by.
|
|
|
|
* @param options The options for sorting.
|
|
|
|
*
|
|
|
|
* @returns A new array containing all elements sorted by the selector.
|
|
|
|
*
|
|
|
|
* @example Usage
|
2023-11-21 12:37:43 +00:00
|
|
|
* ```ts
|
2024-04-29 02:57:30 +00:00
|
|
|
* import { sortBy } from "@std/collections/sort-by";
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assertEquals } from "@std/assert";
|
2023-11-21 12:37:43 +00:00
|
|
|
*
|
|
|
|
* const people = [
|
|
|
|
* { name: "Anna" },
|
|
|
|
* { name: "Kim" },
|
|
|
|
* { name: "John" },
|
|
|
|
* ];
|
|
|
|
* const sortedByName = sortBy(people, (it) => it.name);
|
2024-06-03 04:10:27 +00:00
|
|
|
*
|
|
|
|
* assertEquals(sortedByName, [
|
|
|
|
* { name: "Anna" },
|
|
|
|
* { name: "John" },
|
|
|
|
* { name: "Kim" },
|
|
|
|
* ]);
|
2024-05-06 07:51:20 +00:00
|
|
|
* ```
|
2023-11-21 12:37:43 +00:00
|
|
|
*/
|
|
|
|
export function 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
|
2024-05-06 07:51:20 +00:00
|
|
|
* element. Ascending or descending order can be specified through the `order`
|
|
|
|
* option. By default, the elements are sorted in ascending order.
|
2023-11-21 12:37:43 +00:00
|
|
|
*
|
2024-05-20 07:34:47 +00:00
|
|
|
* @typeParam T The type of the array elements.
|
2024-05-06 07:51:20 +00:00
|
|
|
*
|
|
|
|
* @param array The array to sort.
|
|
|
|
* @param selector The selector function to get the value to sort by.
|
|
|
|
* @param options The options for sorting.
|
|
|
|
*
|
|
|
|
* @returns A new array containing all elements sorted by the selector.
|
|
|
|
*
|
|
|
|
* @example Usage
|
2023-11-21 12:37:43 +00:00
|
|
|
* ```ts
|
2024-04-29 02:57:30 +00:00
|
|
|
* import { sortBy } from "@std/collections/sort-by";
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assertEquals } from "@std/assert";
|
2023-11-21 12:37:43 +00:00
|
|
|
*
|
|
|
|
* const people = [
|
|
|
|
* { name: "Anna", age: 34n },
|
|
|
|
* { name: "Kim", age: 42n },
|
|
|
|
* { name: "John", age: 23n },
|
|
|
|
* ];
|
2024-05-06 07:51:20 +00:00
|
|
|
*
|
|
|
|
* const sortedByAge = sortBy(people, (person) => person.age);
|
|
|
|
*
|
|
|
|
* assertEquals(sortedByAge, [
|
|
|
|
* { name: "John", age: 23n },
|
|
|
|
* { name: "Anna", age: 34n },
|
|
|
|
* { name: "Kim", age: 42n },
|
|
|
|
* ]);
|
2023-11-21 12:37:43 +00:00
|
|
|
* ```
|
|
|
|
*/
|
|
|
|
|
|
|
|
export function 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
|
2024-05-06 07:51:20 +00:00
|
|
|
* element. Ascending or descending order can be specified through the `order`
|
|
|
|
* option. By default, the elements are sorted in ascending order.
|
|
|
|
*
|
2024-05-20 07:34:47 +00:00
|
|
|
* @typeParam T The type of the array elements.
|
2024-05-06 07:51:20 +00:00
|
|
|
*
|
|
|
|
* @param array The array to sort.
|
|
|
|
* @param selector The selector function to get the value to sort by.
|
|
|
|
* @param options The options for sorting.
|
|
|
|
*
|
|
|
|
* @returns A new array containing all elements sorted by the selector.
|
2023-11-21 12:37:43 +00:00
|
|
|
*
|
2024-05-06 07:51:20 +00:00
|
|
|
* @example Usage
|
2023-11-21 12:37:43 +00:00
|
|
|
* ```ts
|
2024-04-29 02:57:30 +00:00
|
|
|
* import { sortBy } from "@std/collections/sort-by";
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assertEquals } from "@std/assert";
|
2023-11-21 12:37:43 +00:00
|
|
|
*
|
|
|
|
* 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") },
|
|
|
|
* ];
|
2024-05-06 07:51:20 +00:00
|
|
|
*
|
|
|
|
* 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") },
|
|
|
|
* ]);
|
2023-11-21 12:37:43 +00:00
|
|
|
* ```
|
|
|
|
*/
|
|
|
|
export function sortBy<T>(
|
|
|
|
array: readonly T[],
|
|
|
|
selector: (el: T) => Date,
|
|
|
|
options?: SortByOptions,
|
|
|
|
): T[];
|
|
|
|
export function sortBy<T>(
|
|
|
|
array: readonly T[],
|
|
|
|
selector:
|
|
|
|
| ((el: T) => number)
|
|
|
|
| ((el: T) => string)
|
|
|
|
| ((el: T) => bigint)
|
|
|
|
| ((el: T) => Date),
|
2023-06-07 05:09:28 +00:00
|
|
|
options?: SortByOptions,
|
2021-08-12 10:10:59 +00:00
|
|
|
): T[] {
|
2021-08-27 18:19:57 +00:00
|
|
|
const len = array.length;
|
|
|
|
const indexes = new Array<number>(len);
|
|
|
|
const selectors = new Array<ReturnType<typeof selector> | null>(len);
|
2023-06-07 05:09:28 +00:00
|
|
|
const order = options?.order ?? "asc";
|
2021-08-03 15:32:47 +00:00
|
|
|
|
2024-05-07 07:11:55 +00:00
|
|
|
array.forEach((element, index) => {
|
|
|
|
indexes[index] = index;
|
|
|
|
const selected = selector(element);
|
|
|
|
selectors[index] = Number.isNaN(selected) ? null : selected;
|
2024-02-02 06:01:54 +00:00
|
|
|
});
|
2021-08-06 02:21:23 +00:00
|
|
|
|
2021-08-27 18:19:57 +00:00
|
|
|
indexes.sort((ai, bi) => {
|
2024-02-02 06:01:54 +00:00
|
|
|
let a = selectors[ai]!;
|
|
|
|
let b = selectors[bi]!;
|
2023-06-07 05:09:28 +00:00
|
|
|
if (order === "desc") {
|
|
|
|
[a, b] = [b, a];
|
|
|
|
}
|
2021-08-27 18:19:57 +00:00
|
|
|
if (a === null) return 1;
|
|
|
|
if (b === null) return -1;
|
|
|
|
return a > b ? 1 : a < b ? -1 : 0;
|
|
|
|
});
|
2021-08-03 15:32:47 +00:00
|
|
|
|
2021-08-27 18:19:57 +00:00
|
|
|
for (let i = 0; i < len; i++) {
|
2024-05-07 07:11:55 +00:00
|
|
|
(indexes as unknown as T[])[i] = array[indexes[i]!] as T;
|
2021-08-27 18:19:57 +00:00
|
|
|
}
|
2021-08-05 02:51:21 +00:00
|
|
|
|
2021-08-27 18:19:57 +00:00
|
|
|
return indexes as unknown as T[];
|
2021-08-03 15:32:47 +00:00
|
|
|
}
|