From 80c899035b8a1561feeb24a38f5607d00d250625 Mon Sep 17 00:00:00 2001 From: Felipe Trost Date: Thu, 23 Nov 2023 21:56:46 +0100 Subject: [PATCH] Sort matches before highlighting text --- src/management-system-v2/lib/useFuzySearch.tsx | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/management-system-v2/lib/useFuzySearch.tsx b/src/management-system-v2/lib/useFuzySearch.tsx index 1e5019019..0330db317 100644 --- a/src/management-system-v2/lib/useFuzySearch.tsx +++ b/src/management-system-v2/lib/useFuzySearch.tsx @@ -20,7 +20,9 @@ function highlightText( const result: JSX.Element[] = []; let lastIndex = 0; - for (const [start, end] of matches.indices) { + const sortedMatches = matches.indices.toSorted((a, b) => a[0] - b[0]); + + for (const [start, end] of sortedMatches) { if (lastIndex < start) result.push({value.slice(lastIndex, start)});