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

Implement collections/sortBy ( closes #1061 ) #1069

Merged
merged 5 commits into from
Aug 3, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
49 changes: 49 additions & 0 deletions collections/sort_by.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.

/**
* Returns all elements in the given collection, sorted by their result using the given selector
*
* Example:
*
* ```ts
* import { sortBy } from "./sort_by.ts"
*
* const people = [
* { name: 'Anna', age: 34 },
* { name: 'Kim', age: 42 },
* { name: 'John', age: 23 },
* ]
* const sortedByAge = sortBy(people, it => it.age)
LionC marked this conversation as resolved.
Show resolved Hide resolved
*
* console.assert(sortedByAge === [
* { name: 'John', age: 23 },
* { name: 'Anna', age: 34 },
* { name: 'Kim', age: 42 },
* ])
* ```
*/
export function sortBy<T>(
array: Array<T>,
selector:
| ((el: T) => number)
| ((el: T) => string)
| ((el: T) => bigint)
| ((el: T) => Date),
): Array<T> {
const ret = Array.from(array);

return ret.sort((a, b) => {
const selectedA = selector(a);
const selectedB = selector(b);

if (selectedA > selectedB) {
return 1;
}

if (selectedA < selectedB) {
return -1;
}

return 0;
});
}
104 changes: 104 additions & 0 deletions collections/sort_by_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.

import { assertEquals } from "../testing/asserts.ts";
import { sortBy } from "./sort_by.ts";

function sortByTest<T>(
input: [
Array<T>,
| ((el: T) => number)
| ((el: T) => string)
| ((el: T) => bigint)
| ((el: T) => Date),
],
expected: Array<T>,
message?: string,
) {
const actual = sortBy(...input);
assertEquals(actual, expected, message);
}

Deno.test({
name: "[collections/sortBy] no mutation",
fn() {
const array = ["a", "abc", "ba"];
sortBy(array, (it) => it.length);

assertEquals(array, ["a", "abc", "ba"]);
},
});

Deno.test({
name: "[collections/sortBy] empty input",
fn() {
sortByTest(
[[], () => 5],
[],
);
},
});

Deno.test({
name: "[collections/sortBy] identity selector",
fn() {
sortByTest(
[
[2, 3, 1],
(it) => it,
],
[1, 2, 3],
);
},
});

Deno.test({
name: "[collections/sortBy] sortings",
fn() {
const testArray = [
{ name: "benchmark", stage: 3 },
{ name: "test", stage: 2 },
{ name: "build", stage: 1 },
{ name: "deploy", stage: 4 },
];

sortByTest(
[
testArray,
(it) => it.stage,
],
[
{ name: "build", stage: 1 },
{ name: "test", stage: 2 },
{ name: "benchmark", stage: 3 },
{ name: "deploy", stage: 4 },
],
);
sortByTest(
[
testArray,
(it) => it.name,
],
[
{ name: "benchmark", stage: 3 },
{ name: "build", stage: 1 },
{ name: "deploy", stage: 4 },
{ name: "test", stage: 2 },
],
);
sortByTest(
[
[
"February 1, 2022",
"December 17, 1995",
"June 12, 2012",
],
(it) => new Date(it),
],
[
"December 17, 1995",
"June 12, 2012",
"February 1, 2022",
],
);
},
});