From 8f963ab78a6351867ffa2ba35b6830aa2f13c209 Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Wed, 9 Oct 2024 01:45:38 +0200 Subject: [PATCH] perf: refs #7793 imrpove sort data method --- src/components/common/VnSelect.vue | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/components/common/VnSelect.vue b/src/components/common/VnSelect.vue index 345505a71..85a8a668d 100644 --- a/src/components/common/VnSelect.vue +++ b/src/components/common/VnSelect.vue @@ -139,6 +139,23 @@ function findKeyInOptions() { } function setOptions(data) { + data = data.sort((a, b) => { + const search = lastVal.value?.toString()?.toLowerCase(); + const aValue = String(a[$props.optionLabel]).toLowerCase(); + const bValue = String(b[$props.optionLabel]).toLowerCase(); + + const aIndex = aValue.indexOf(search); + const bIndex = bValue.indexOf(search); + + const aPriority = aIndex === 0 ? 0 : aIndex > 0 ? 1 : 2; + const bPriority = bIndex === 0 ? 0 : bIndex > 0 ? 1 : 2; + + if (aPriority !== bPriority) { + return aPriority - bPriority; + } + + return aValue.localeCompare(bValue); + }); myOptions.value = JSON.parse(JSON.stringify(data)); myOptionsOriginal.value = JSON.parse(JSON.stringify(data)); }