Skip to content

Commit 47bdcfc

Browse files
committed
prefix matching is preffered in search function
1 parent d18cc83 commit 47bdcfc

1 file changed

Lines changed: 13 additions & 2 deletions

File tree

my-app/src/presenters/SearchbarPresenter.jsx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
 (0)