-
Notifications
You must be signed in to change notification settings - Fork 0
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
5 changed files
with
74 additions
and
7 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
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
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
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,26 @@ | ||
import { describe, expect, it } from 'vitest' | ||
|
||
import { sort } from './sort' | ||
|
||
describe('sort', () => { | ||
it('sorts numbers in ascending order', () => { | ||
expect(sort([3, 1, 2])).toEqual([1, 2, 3]) | ||
}) | ||
it('sorts numbers in descending order', () => { | ||
expect(sort([3, 1, 2], { desc: true })).toEqual([3, 2, 1]) | ||
}) | ||
it('sorts objects in ascending order', () => { | ||
const arr = [{ value: 3 }, { value: 1 }, { value: 2 }] | ||
expect(sort(arr, { getter: item => item.value })).toEqual([{ value: 1 }, { value: 2 }, { value: 3 }]) | ||
}) | ||
it('sorts objects in descending order', () => { | ||
const arr = [{ value: 3 }, { value: 1 }, { value: 2 }] | ||
expect(sort(arr, { getter: item => item.value, desc: true })).toEqual([{ value: 3 }, { value: 2 }, { value: 1 }]) | ||
}) | ||
it('return a new array', () => { | ||
const arr = [3, 1, 2] | ||
const sorted = sort(arr) | ||
expect(sorted).toEqual([1, 2, 3]) | ||
expect(sorted).not.toBe(arr) | ||
}) | ||
}) |
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,38 @@ | ||
interface SortOptions<T> { | ||
getter?: (item: T) => number | ||
desc?: boolean | ||
} | ||
|
||
/** | ||
* Sorts an array of objects or numbers based on a specified getter function. | ||
* | ||
* @template T - The type of the array elements. | ||
* @param {readonly T[]} arr - The array to be sorted. | ||
* @param {SortOptions<T>} [options={}] - The sorting options. | ||
* @param {Function} [options.getter] - The getter function to extract the value to be used for sorting. | ||
* @param {boolean} [options.desc=false] - Specifies whether to sort in descending order. | ||
* @returns {T[]} - The sorted array. | ||
* @example | ||
* ```ts | ||
* sort([3, 1, 2]) // [1, 2, 3] | ||
* sort([3, 1, 2], { desc: true }) // [3, 2, 1] | ||
* sort([{ value: 3 }, { value: 1 }, { value: 2 }], { getter: item => item.value }) // [{ value: 1 }, { value: 2 }, { value: 3 }] | ||
* ``` | ||
*/ | ||
export function sort<T extends number>(arr: readonly T[], options?: SortOptions<T>): T[] | ||
export function sort<T extends object>( | ||
arr: readonly T[], | ||
options: SortOptions<T> | ||
): T[] | ||
export function sort<T extends object | number>( | ||
arr: readonly T[], | ||
options: SortOptions<T> = {}, | ||
): T[] { | ||
const { getter, desc = false } = options | ||
|
||
const getter_ = (item: T) => getter ? getter(item) : item as number | ||
const asc = (a: T, b: T) => getter_(a) - getter_(b) | ||
const dsc = (a: T, b: T) => getter_(b) - getter_(a) | ||
|
||
return arr.slice().sort(desc ? dsc : asc) | ||
} |