Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions lib/utils/sort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,20 @@ export const sortByKeyValue = <T extends TObject>({
key,
order = 'ASC'
}: SortParams<T>) => {
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;
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down