From cb459d937a314390295dcf87adabf45010ad9aca Mon Sep 17 00:00:00 2001 From: doowb Date: Sun, 10 Sep 2017 18:58:12 -0400 Subject: [PATCH] add implementation for null and undefined values --- index.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 621340f..91e23ca 100644 --- a/index.js +++ b/index.js @@ -91,11 +91,23 @@ function compare(prop, a, b) { /** * Default compare function used as a fallback - * for sorting. + * for sorting. Built-in array sorting pushes + * null and undefined values to the end of the array. */ function defaultCompare(a, b) { - return a < b ? -1 : (a > b ? 1 : 0); + var typeA = typeOf(a); + var typeB = typeOf(b); + + if (typeA === 'null') { + return typeB === 'null' ? 0 : (typeB === 'undefined' ? -1 : 1); + } else if (typeA === 'undefined') { + return typeB === 'null' ? 1 : (typeB === 'undefined' ? 0 : 1); + } else if (typeB === 'null' || typeB === 'undefined') { + return -1; + } else { + return a < b ? -1 : (a > b ? 1 : 0); + } } /**