From 654d588e27f1ae695981009bacedea420a4fba92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hanno=20J=2E=20G=C3=B6decke?= Date: Fri, 28 Mar 2025 14:44:07 +0100 Subject: [PATCH 1/2] fix: sort search terms actually by length --- src/hooks/useFastSearchFromOptions.ts | 6 ++++-- src/libs/StringUtils.ts | 10 +++++++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/hooks/useFastSearchFromOptions.ts b/src/hooks/useFastSearchFromOptions.ts index f1e318cde990..7ab283ab982b 100644 --- a/src/hooks/useFastSearchFromOptions.ts +++ b/src/hooks/useFastSearchFromOptions.ts @@ -1,4 +1,5 @@ import deburr from 'lodash/deburr'; +import StringUtils from '@libs/StringUtils'; import {useMemo} from 'react'; import FastSearch from '@libs/FastSearch'; import {filterUserToInvite, isSearchStringMatch} from '@libs/OptionsListUtils'; @@ -64,8 +65,9 @@ function useFastSearchFromOptions( ]); function search(searchInput: string): AllOrSelectiveOptions { - const searchWords = deburr(searchInput).split(' ').sort(); // asc sorted - const longestSearchWord = searchWords.at(searchWords.length - 1); // longest word is the last element + const searchWords = deburr(searchInput).split(' '); + const searchWordsSorted = StringUtils.sortStringArrayByLength(searchWords); + const longestSearchWord = searchWordsSorted.at(searchWordsSorted.length - 1); // longest word is the last element if (!longestSearchWord) { return emptyResult; } diff --git a/src/libs/StringUtils.ts b/src/libs/StringUtils.ts index 385553563ec3..95ee35cc074d 100644 --- a/src/libs/StringUtils.ts +++ b/src/libs/StringUtils.ts @@ -106,4 +106,12 @@ function removeDoubleQuotes(text = '') { return text.replace(/"/g, ''); } -export default {sanitizeString, isEmptyString, removeInvisibleCharacters, normalizeAccents, normalizeCRLF, lineBreaksToSpaces, getFirstLine, removeDoubleQuotes}; +/** + * Sort an array of strings by their length. + * The longest strings will be at the end of the array. + */ +function sortStringArrayByLength(arr: string[]): string[] { + return arr.sort((a, b) => a.length - b.length); +} + +export default {sanitizeString, isEmptyString, removeInvisibleCharacters, normalizeAccents, normalizeCRLF, lineBreaksToSpaces, getFirstLine, removeDoubleQuotes, sortStringArrayByLength}; From 9e38bbdf1018ddc245d229596d169f6e0a5e951b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hanno=20J=2E=20G=C3=B6decke?= Date: Sun, 30 Mar 2025 10:46:00 +0200 Subject: [PATCH 2/2] prettier --- src/hooks/useFastSearchFromOptions.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hooks/useFastSearchFromOptions.ts b/src/hooks/useFastSearchFromOptions.ts index 7ab283ab982b..73a8cbec71d5 100644 --- a/src/hooks/useFastSearchFromOptions.ts +++ b/src/hooks/useFastSearchFromOptions.ts @@ -1,9 +1,9 @@ import deburr from 'lodash/deburr'; -import StringUtils from '@libs/StringUtils'; import {useMemo} from 'react'; import FastSearch from '@libs/FastSearch'; import {filterUserToInvite, isSearchStringMatch} from '@libs/OptionsListUtils'; import type {Options as OptionsListType, ReportAndPersonalDetailOptions} from '@libs/OptionsListUtils'; +import StringUtils from '@libs/StringUtils'; type AllOrSelectiveOptions = ReportAndPersonalDetailOptions | OptionsListType;