This repository has been archived by the owner on Jan 20, 2024. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { sortByAttrs } from "./sort"; | ||
|
||
type Record = { | ||
id: number; | ||
name: string; | ||
}; | ||
|
||
const testData: Record[] = [ | ||
{ id: 1, name: "Beth" }, | ||
{ id: 2, name: "Anna" }, | ||
{ id: 3, name: "Roy" }, | ||
{ id: 4, name: "Amy" }, | ||
]; | ||
const sortedByName: Record[] = [ | ||
{ id: 2, name: "Anna" }, | ||
{ id: 4, name: "Amy" }, | ||
{ id: 1, name: "Beth" }, | ||
{ id: 3, name: "Roy" }, | ||
]; | ||
const sortedByID: Record[] = [ | ||
{ id: 1, name: "Beth" }, | ||
{ id: 2, name: "Anna" }, | ||
{ id: 3, name: "Roy" }, | ||
{ id: 4, name: "Amy" }, | ||
]; | ||
const testDataWithMultipleProps: Record[] = [ | ||
{ id: 2, name: "Beth" }, | ||
{ id: 1, name: "Beth" }, | ||
{ id: 3, name: "Roy" }, | ||
{ id: 3, name: "Amy" }, | ||
]; | ||
const sortedByMultipleProps: Record[] = [ | ||
{ id: 1, name: "Beth" }, | ||
{ id: 2, name: "Beth" }, | ||
{ id: 3, name: "Amy" }, | ||
{ id: 3, name: "Roy" }, | ||
]; | ||
|
||
describe("sort helpers on strict types", () => { | ||
it("generates an empty array in response to an empty array", () => { | ||
expect(sortByAttrs([])).toEqual([]); | ||
}); | ||
it("sorts an array by a single property", () => { | ||
expect(sortByAttrs(testData, "name")).toEqual(sortedByName); | ||
expect(sortByAttrs(testData, "id")).toEqual(sortedByID); | ||
}); | ||
it("sorts an array by multiple props", () => { | ||
expect(sortByAttrs(testDataWithMultipleProps, "name", "id")).toEqual( | ||
sortedByMultipleProps | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
type Map = { [key: string]: any }; | ||
|
||
/** | ||
* Sorts any array given the list of attributes in order of descending priority. | ||
* Uses bubble sort algorithm to sort the array. Returns a new array. | ||
* @param a The array of things to sort | ||
* @param attrs The attributes in order of descending priority | ||
*/ | ||
export const sortByAttrs = <T extends Map>(a: T[], ...attrs: (keyof T)[]) => { | ||
// the single edge case | ||
if (a.length === 0) { | ||
return []; | ||
} | ||
const arr = [...a]; | ||
for (let i = 0; i < arr.length; i++) { | ||
for (let j = i; j < arr.length - 1; j++) { | ||
attrs.forEach((attr) => { | ||
if (arr[j][attr] > arr[j + 1][attr]) { | ||
let temp = arr[j]; | ||
arr[j] = arr[j + 1]; | ||
arr[j + 1] = temp; | ||
} | ||
}); | ||
} | ||
} | ||
return arr; | ||
}; |