diff --git a/packages/webapp/src/containers/Animals/Inventory/useAnimalInventory.tsx b/packages/webapp/src/containers/Animals/Inventory/useAnimalInventory.tsx index b2f8275929..5d9ee54199 100644 --- a/packages/webapp/src/containers/Animals/Inventory/useAnimalInventory.tsx +++ b/packages/webapp/src/containers/Animals/Inventory/useAnimalInventory.tsx @@ -44,6 +44,7 @@ export type AnimalInventory = { id: string; iconName: AnimalTypeIconKey; identification: string; + name: string | null; type: string; breed: string; groups: string[]; @@ -157,6 +158,7 @@ const formatAnimalsData = ( path: createSingleAnimalViewURL(animal.internal_identifier), count: 1, batch: false, + name: animal.name, // preserve some untransformed data for filtering group_ids: animal.group_ids, sex_id: animal.sex_id, @@ -192,6 +194,7 @@ const formatAnimalBatchesData = ( groups: batch.group_ids.map((id: number) => getProperty(animalGroups, id, 'name')), path: createSingleAnimalViewURL(batch.internal_identifier), count: batch.count, + name: batch.name, batch: true, // preserve some untransformed data for filtering group_ids: batch.group_ids, @@ -214,6 +217,22 @@ interface BuildInventoryArgs { defaultAnimalTypes: DefaultAnimalType[]; } +const sortAnimalsIDs = (inventory: AnimalInventory[]) => { + return inventory.sort((a, b) => { + if (a.name && !b.name) { + return -1; + } else if (!a.name && b.name) { + return 1; + } else { + if (a.identification.length > b.identification.length) { + return 1; + } else { + return a.identification.localeCompare(b.identification); + } + } + }); +}; + export const buildInventory = ({ animals, animalBatches, @@ -242,7 +261,7 @@ export const buildInventory = ({ ), ]; - const sortedInventory = inventory.sort(getComparator(orderEnum.ASC, 'identification')); + const sortedInventory = sortAnimalsIDs(inventory); return sortedInventory; };