Skip to content

Commit

Permalink
Improves sortBy
Browse files Browse the repository at this point in the history
  • Loading branch information
djalmajr committed Apr 27, 2023
1 parent b937b2d commit e9425f9
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/sortBy.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { curry } from './curry';
import { get } from './get';

interface SortByFn {
<T extends object>(key: keyof T): (b: T, c: T) => number;
Expand All @@ -10,5 +11,6 @@ interface SortByFn {
* TODO: adicionar documentação
*/
export const sortBy = curry(function <T extends object>(k: keyof T, a: T, b: T) {
return (a[k] > b[k] && 1) || (a[k] < b[k] && -1) || 0;
const v = get<T>(k as string);
return (v(a) > v(b) && 1) || (v(a) < v(b) && -1) || 0;
}) as SortByFn;

0 comments on commit e9425f9

Please sign in to comment.