feat: create function to sort an array of objects by property
Summary: Test Plan:
This commit is contained in:
15
src/libs/sortByProperty.ts
Normal file
15
src/libs/sortByProperty.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
export function sortByProperty(
|
||||
priorityObj: Record<string, number>,
|
||||
property: string,
|
||||
) {
|
||||
return (objA: Record<string, any>, objB: Record<string, any>) => {
|
||||
const priorityA = priorityObj[objA[property]];
|
||||
const priorityB = priorityObj[objB[property]];
|
||||
|
||||
if (priorityA && !priorityB) return -1;
|
||||
if (!priorityA && priorityB) return 1;
|
||||
if (priorityA && priorityB) return priorityA - priorityB;
|
||||
|
||||
return 0;
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user