@@ -48,10 +48,36 @@ export const useSearchMatchingEquipments = (props: UseSearchMatchingEquipmentsPr
4848 fetchElements,
4949 } ) ;
5050
51- const equipmentsFound = useMemo (
52- ( ) => getEquipmentsInfosForSearchBar ( elementsFound , getNameOrId ) ,
53- [ elementsFound , getNameOrId ]
54- ) ;
51+ const equipmentsFound = useMemo ( ( ) => {
52+ const equipments = getEquipmentsInfosForSearchBar ( elementsFound , getNameOrId ) ;
53+
54+ if ( ! searchTerm ) {
55+ // Keep original sort (substations first, then voltage levels ...)
56+ return equipments ;
57+ }
58+
59+ // group by equipment type, prioritize items starting with search term within each type
60+ const term = searchTerm . toLowerCase ( ) ;
61+ return equipments . toSorted ( ( a , b ) => {
62+ // maintain equipment type order
63+ if ( a . type !== b . type ) {
64+ const aIndex = equipments . findIndex ( ( e ) => e . type === a . type ) ;
65+ const bIndex = equipments . findIndex ( ( e ) => e . type === b . type ) ;
66+ return aIndex - bIndex ;
67+ }
68+
69+ // within same type, prioritize items starting with search term
70+ const aStarts = a . label . toLowerCase ( ) . startsWith ( term ) ;
71+ const bStarts = b . label . toLowerCase ( ) . startsWith ( term ) ;
72+
73+ if ( aStarts !== bStarts ) {
74+ return aStarts ? - 1 : 1 ;
75+ }
76+
77+ // maintain original order within same type and same start status
78+ return equipments . indexOf ( a ) - equipments . indexOf ( b ) ;
79+ } ) ;
80+ } , [ elementsFound , getNameOrId , searchTerm ] ) ;
5581
5682 useEffect ( ( ) => {
5783 updateSearchTerm ( searchTerm ?. trim ( ) ) ;
0 commit comments