From 5ef006354124354cd611075652a64164f45e1136 Mon Sep 17 00:00:00 2001 From: Esaul Franco Date: Sat, 27 Sep 2025 08:57:57 -0600 Subject: [PATCH 1/2] Refactor sorting function to improve type handling and performance --- lib/utils/sort.ts | 21 ++++++++++++++++----- package.json | 2 +- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/lib/utils/sort.ts b/lib/utils/sort.ts index 8dcbf6c..fdcd69b 100644 --- a/lib/utils/sort.ts +++ b/lib/utils/sort.ts @@ -18,11 +18,22 @@ export const sortByKeyValue = ({ key, order = 'ASC' }: SortParams) => { - const sortedArray = array.slice().sort((a, b) => { - return String(getKeyValue({ object: a, key })).toLowerCase() >= - String(getKeyValue({ object: b, key })).toLowerCase() - ? 1 - : -1; + const sortedArray = array.toSorted((a, b) => { + const aValue = getKeyValue({ object: a, key }); + const bValue = getKeyValue({ object: b, key }); + + if (typeof aValue === 'number' && typeof bValue === 'number') { + return aValue - bValue; + } + if (aValue instanceof Date && bValue instanceof Date) { + return aValue.getTime() - bValue.getTime(); + } + if (typeof aValue === 'boolean' && typeof bValue === 'boolean') { + return Number(aValue) - Number(bValue); + } + return String(aValue).toLowerCase() <= String(bValue).toLowerCase() + ? -1 + : 1; }); return order === 'ASC' ? sortedArray : sortedArray.reverse(); diff --git a/package.json b/package.json index 9e0a361..1cd11c6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@nuc-lib/deep-key", - "version": "2.0.1", + "version": "2.0.2", "description": "Utility to work with deep keys in objects.", "author": "EsaĂșl Franco", "license": "MIT", From 4cd6edd59e895310ac62f849b96d2b7e2ec619c5 Mon Sep 17 00:00:00 2001 From: Esaul Franco Date: Sat, 27 Sep 2025 09:01:58 -0600 Subject: [PATCH 2/2] Fix sorting logic to ensure correct order for non-numeric values --- lib/utils/sort.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/utils/sort.ts b/lib/utils/sort.ts index fdcd69b..c6252ca 100644 --- a/lib/utils/sort.ts +++ b/lib/utils/sort.ts @@ -31,9 +31,9 @@ export const sortByKeyValue = ({ if (typeof aValue === 'boolean' && typeof bValue === 'boolean') { return Number(aValue) - Number(bValue); } - return String(aValue).toLowerCase() <= String(bValue).toLowerCase() - ? -1 - : 1; + return String(aValue).toLowerCase() >= String(bValue).toLowerCase() + ? 1 + : -1; }); return order === 'ASC' ? sortedArray : sortedArray.reverse();