Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(collections/sort_by): descending order can be specified in options #3419

Merged
merged 4 commits into from
Jun 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions collections/sort_by.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.

/** Order */
export type Order = "asc" | "desc";

/** Options for sortBy */
export type SortByOptions = {
order: Order;
};

/**
* 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.
* element. Ascending or descending order can be specified.
*
* @example
* ```ts
Expand All @@ -23,23 +31,35 @@
* { 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 },
* ]);
* ```
*/
export function sortBy<T>(
array: readonly T[],
selector: (el: T) => number,
options?: SortByOptions,
): T[];
export function sortBy<T>(
array: readonly T[],
selector: (el: T) => string,
options?: SortByOptions,
): T[];
export function sortBy<T>(
array: readonly T[],
selector: (el: T) => bigint,
options?: SortByOptions,
): T[];
export function sortBy<T>(
array: readonly T[],
selector: (el: T) => Date,
options?: SortByOptions,
): T[];
export function sortBy<T>(
array: readonly T[],
Expand All @@ -48,10 +68,12 @@ export function sortBy<T>(
| ((el: T) => string)
| ((el: T) => bigint)
| ((el: T) => Date),
options?: SortByOptions,
): T[] {
const len = array.length;
const indexes = new Array<number>(len);
const selectors = new Array<ReturnType<typeof selector> | null>(len);
const order = options?.order ?? "asc";

for (let i = 0; i < len; i++) {
indexes[i] = i;
Expand All @@ -60,8 +82,11 @@ export function sortBy<T>(
}

indexes.sort((ai, bi) => {
const a = selectors[ai];
const b = selectors[bi];
let a = selectors[ai];
let b = selectors[bi];
if (order === "desc") {
[a, b] = [b, a];
}
if (a === null) return 1;
if (b === null) return -1;
sant123 marked this conversation as resolved.
Show resolved Hide resolved
return a > b ? 1 : a < b ? -1 : 0;
Expand Down
24 changes: 24 additions & 0 deletions collections/sort_by_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,27 @@ Deno.test({
);
},
});

Deno.test({
name: "[collections/sortBy] desc ordering",
fn() {
assertEquals(
sortBy(
[
"January 27, 1995",
"November 26, 2020",
"June 17, 1952",
"July 15, 1993",
],
(it) => new Date(it),
{ order: "desc" },
),
[
"November 26, 2020",
"January 27, 1995",
"July 15, 1993",
"June 17, 1952",
],
);
},
});