Skip to content

Commit cb459d9

Browse files
committed
add implementation for null and undefined values
1 parent 0770421 commit cb459d9

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

index.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,23 @@ function compare(prop, a, b) {
9191

9292
/**
9393
* Default compare function used as a fallback
94-
* for sorting.
94+
* for sorting. Built-in array sorting pushes
95+
* null and undefined values to the end of the array.
9596
*/
9697

9798
function defaultCompare(a, b) {
98-
return a < b ? -1 : (a > b ? 1 : 0);
99+
var typeA = typeOf(a);
100+
var typeB = typeOf(b);
101+
102+
if (typeA === 'null') {
103+
return typeB === 'null' ? 0 : (typeB === 'undefined' ? -1 : 1);
104+
} else if (typeA === 'undefined') {
105+
return typeB === 'null' ? 1 : (typeB === 'undefined' ? 0 : 1);
106+
} else if (typeB === 'null' || typeB === 'undefined') {
107+
return -1;
108+
} else {
109+
return a < b ? -1 : (a > b ? 1 : 0);
110+
}
99111
}
100112

101113
/**

0 commit comments

Comments
 (0)