From 60bc8e36512f9709be856a54a4565fa90de8bc9a Mon Sep 17 00:00:00 2001 From: Randy Morris Date: Tue, 14 May 2019 04:15:10 +0000 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=90=9B=20sortCollection=20does=20n?= =?UTF-8?q?ot=20sort=20strings=20properly?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit sortCollection fails to sort collections of strings due to the use of "-" in the compareFunction. ✅ Closes: #94 --- dist/index.js | 2 +- src/lib/utils.js | 4 ++-- tests/utils.test.js | 10 ++++++++++ 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/dist/index.js b/dist/index.js index 8fd4f6d..5459cb5 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1 +1 @@ -"use strict";function _interopDefault(e){return e&&"object"==typeof e&&"default"in e?e.default:e}var React=require("react"),React__default=_interopDefault(React);require("prop-types");var debounce=function(r,n){var l=null;return function(){clearTimeout(l);var e=arguments,t=this;l=setTimeout(function(){r.apply(t,e)},n)}};function includes(e,t,r){return r?-1>>0;if("function"!=typeof e)throw new TypeError("predicate must be a function");for(var n=arguments[1],l=0;l>>0;if("function"!=typeof e)throw new TypeError("predicate must be a function");for(var n=arguments[1],l=0;l export const sortCollection = (array, valueKey) => { if (valueKey) { - return array.sort((a, b) => a[valueKey] - b[valueKey]); + return array.sort((a, b) => a[valueKey] < b[valueKey] ? -1 : 1); } else { - return array.sort((a, b) => a - b); + return array.sort((a, b) => a < b ? -1 : 1); } }; diff --git a/tests/utils.test.js b/tests/utils.test.js index 6aa8eb0..03ee646 100644 --- a/tests/utils.test.js +++ b/tests/utils.test.js @@ -99,6 +99,16 @@ describe('Utils', () => { const result = sortCollection(data, 'id'); expect(result).toEqual([{ id: 1 }, { id: 2 }, { id: 3 }]); }); + it('should sort an array of strings ascending', () => { + const data = ["b", "c", "a"]; + const result = sortCollection(data); + expect(result).toEqual(["a", "b", "c"]); + }); + it('should sort object array with string keys ascending', () => { + const data = [{ id: "b" }, { id: "c" }, { id: "a" }]; + const result = sortCollection(data, 'id'); + expect(result).toEqual([{ id: "a" }, { id: "b" }, { id: "c" }]); + }); }); describe('arrays equal', () => { it('should return false if arrays are different lengths', () => {