File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -28,8 +28,19 @@ const SearchbarPresenter = observer(({ model }) => {
2828 model . setCurrentSearch ( model . filteredCourses ) ;
2929 } else {
3030 const fuse = new Fuse ( model . filteredCourses , fuseOptions ) ;
31- const results = fuse . search ( query ) . map ( ( r ) => r . item ) ;
32- model . setCurrentSearch ( results ) ;
31+ const results = fuse . search ( query ) ;
32+
33+ const sortedResults = results . sort ( ( a , b ) => {
34+ const aStartsWith = a . item . code . toLowerCase ( ) . startsWith ( query . toLowerCase ( ) ) ;
35+ const bStartsWith = b . item . code . toLowerCase ( ) . startsWith ( query . toLowerCase ( ) ) ;
36+
37+ //sort by prefix match as a primary sorting
38+ if ( aStartsWith && ! bStartsWith ) return - 1 ;
39+ if ( ! aStartsWith && bStartsWith ) return 1 ;
40+ return a . score - b . score ; //Fuse.js score sorting otherwise
41+ } ) ;
42+
43+ model . setCurrentSearch ( sortedResults . map ( r => r . item ) ) ;
3344 }
3445 } , 500 ) , [ ] ) ;
3546
You can’t perform that action at this time.
0 commit comments