8000 Merge pull request #922 from jseb/master · immutable-js/immutable-js@24c834a · GitHub
[go: up one dir, main page]

Skip to content

Commit 24c834a

Browse files
author
Kevin Lacker
authored
Merge pull request #922 from jseb/master
Improves default sorting comparator when undefined values are present.
2 parents c483ceb + e832a61 commit 24c834a

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-2
lines changed

__tests__/sort.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ describe('sort', () => {
1515
expect(List.of(4,5,6,3,2,1).sort().toArray()).toEqual([1,2,3,4,5,6]);
1616
})
1717

18+
it('sorts undefined values last', () => {
19+
expect(List.of(4,undefined,5,6,3,undefined,2,1).sort().toArray()).toEqual([1,2,3,4,5,6,undefined,undefined]);
20+
})
21+
1822
it('sorts a keyed sequence', () => {
1923
expect(Seq({z:1,y:2,x:3,c:3,b:2,a:1}).sort().entrySeq().toArray())
2024
.toEqual([['z', 1],['a', 1],['y', 2],['b', 2],['x', 3],['c', 3]]);

dist/immutable.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3610,6 +3610,18 @@
36103610
}
36113611

36123612
function defaultComparator(a, b) {
3613+
if (a === undefined && b === undefined) {
3614+
return 0;
3615+
}
3616+
3617+
if (a === undefined) {
3618+
return 1;
3619+
}
3620+
3621+
if (b === undefined) {
3622+
return -1;
3623+
}
3624+
36133625
return a > b ? 1 : a < b ? -1 : 0;
36143626
}
36153627

0 commit comments

Comments
 (0)
0