Function sortBy

  • 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 order option. By default, the elements are sorted in ascending order.

    Note: If you want to process any iterable, use the new version of sortBy from @std/collections/unstable-sort-by.

    Type Parameters

    • T

      The type of the array elements.

    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: SortByOptions

      The options for sorting.

    Returns T[]

    A new array containing all elements sorted by the selector.

    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 },
    ]);
  • 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 order option. By default, the elements are sorted in ascending order.

    Type Parameters

    • T

      The type of the array elements.

    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: SortByOptions

      The options for sorting.

    Returns T[]

    A new array containing all elements sorted by the selector.

    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" },
    ]);
  • 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 order option. By default, the elements are sorted in ascending order.

    Type Parameters

    • T

      The type of the array elements.

    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: SortByOptions

      The options for sorting.

    Returns T[]

    A new array containing all elements sorted by the selector.

    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 },
    ]);
  • 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 order option. By default, the elements are sorted in ascending order.

    Type Parameters

    • T

      The type of the array elements.

    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: SortByOptions

      The options for sorting.

    Returns T[]

    A new array containing all elements sorted by the selector.

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