Skip to content

Commit

Permalink
feat(collections): implement sortBy (#1069)
Browse files Browse the repository at this point in the history
  • Loading branch information
LionC committed Aug 3, 2021
1 parent 933959b commit 94a84bc
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 0 deletions.
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)
*
* 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",
],
);
},
});

0 comments on commit 94a84bc

Please sign in to comment.