diff --git a/lib/utils/sort.ts b/lib/utils/sort.ts index 8dcbf6c..c6252ca 100644 --- a/lib/utils/sort.ts +++ b/lib/utils/sort.ts @@ -18,9 +18,20 @@ 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() + 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; }); 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",