Skip to content

Commit

Permalink
fix: issue sorting using 2-ary comparators
Browse files Browse the repository at this point in the history
  • Loading branch information
poteat committed Dec 15, 2024
1 parent 50863cc commit 59a9e73
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 3 deletions.
8 changes: 8 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## [0.25.2]

- Fix `List.Sort` to work with 2-ary comparators.

## [0.25.1]

- Fix packaging issue.

## [0.25.0]

- Reify `NaturalNumber.Modulo` and `NaturalNumber.ModuloBy` to value-level functions.
Expand Down
2 changes: 1 addition & 1 deletion pkg/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hkt-toolbelt",
"version": "0.25.0",
"version": "0.25.2",
"description": "Functional and composable type utilities",
"types": "./index.d.ts",
"main": "./index.js",
Expand Down
39 changes: 38 additions & 1 deletion src/list/sort.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import { $, Test, List, String, Function } from '..'
import {
$,
Test,
List,
String,
Function,
Kind,
NaturalNumber,
Integer
} from '..'

type Sort_Spec = [
/**
Expand Down Expand Up @@ -46,3 +55,31 @@ it('should sort a list of strings by length', () => {
it('should sort an empty list', () => {
expect(List.sort(Function.identity)([])).toEqual([])
})

it('can sort complicated functions on lists', () => {
/**
* Sort descending by number, then ascending by string.
*/
const mySort = List.sort(
List.compareBy([
Kind.lazyPipe([NaturalNumber.compare, Integer.negate]),
String.compare
])
)

const result = mySort([
[1, 'foo'],
[2, 'bar'],
[3, 'baz'],
[1, 'qux'],
[3, 'abc']
])

expect(result).toEqual([
[3, 'abc'],
[3, 'baz'],
[2, 'bar'],
[1, 'foo'],
[1, 'qux']
])
})
2 changes: 1 addition & 1 deletion src/list/sort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ export const sort = ((f: Function.Function) => (values: unknown[]) => {
const result = f(a as never) as number | Function.Function

return typeof result === 'function'
? (result(a as never) as number)
? (result(b as never) as number)
: result - (f(b as never) as number)
})

Expand Down

0 comments on commit 59a9e73

Please sign in to comment.