@@ -103,17 +103,51 @@ function filter() {
103103 return ;
104104 }
105105
106+ const visibleElements = [ ] ;
107+ const nameFilterValue = FILTERS . name . element . value . toLowerCase ( ) ;
108+
106109 for ( const element of commandElements ) {
110+ let shouldHide = false ;
111+
107112 for ( const [ key , filter ] of Object . entries ( FILTERS ) ) {
108113 if ( ! filter . element . value ) continue ;
109114
110115 const elementValue = key == 'alpha' ? element . dataset [ 'name' ] : element . dataset [ key ] ;
111116 if ( ! match ( filter , elementValue ) ) {
112117 element . style . display = 'none' ;
113118 hiddenCards . push ( element ) ;
119+ shouldHide = true ;
114120 break ;
115121 }
116122 }
123+
124+ if ( ! shouldHide ) {
125+ visibleElements . push ( element ) ;
126+ }
127+ }
128+
129+ // Sort visible elements: commands starting with search term first, then others
130+ if ( nameFilterValue ) {
131+ const grid = document . querySelector ( '#commands-grid' ) ;
132+
133+ visibleElements . sort ( ( a , b ) => {
134+ const aName = a . dataset . name . toLowerCase ( ) ;
135+ const bName = b . dataset . name . toLowerCase ( ) ;
136+ const aStarts = aName . startsWith ( nameFilterValue ) ;
137+ const bStarts = bName . startsWith ( nameFilterValue ) ;
138+
139+ // If one starts with the search term and the other doesn't, prioritize the one that starts
140+ if ( aStarts && ! bStarts ) return - 1 ;
141+ if ( ! aStarts && bStarts ) return 1 ;
142+
143+ // Otherwise maintain alphabetical order
144+ return aName . localeCompare ( bName ) ;
145+ } ) ;
146+
147+ // Re-append elements in sorted order
148+ visibleElements . forEach ( element => {
149+ grid . appendChild ( element ) ;
150+ } ) ;
117151 }
118152}
119153
0 commit comments