@@ -373,25 +373,47 @@ export function compareItemsByScore<T>(itemA: T, itemB: T, query: string, access
373373 return scoreA > scoreB ? - 1 : 1 ;
374374 }
375375
376- // 6.) at this point, scores are identical for both paths
376+ // 6.) at this point, scores are identical for both items so we start to sort by length
377377
378+ // check for label + description length and prefer shorter
378379 const labelA = accessor . getItemLabel ( itemA ) ;
379380 const labelB = accessor . getItemLabel ( itemB ) ;
380381
381- if ( labelA . length !== labelB . length ) {
382- return labelA . length - labelB . length ; // prefer shorter labels
382+ const descriptionA = accessor . getItemDescription ( itemA ) ;
383+ const descriptionB = accessor . getItemDescription ( itemB ) ;
384+
385+ const labelDescriptionALength = labelA . length + ( descriptionA ? descriptionA . length : 0 ) ;
386+ const labelDescriptionBLength = labelB . length + ( descriptionB ? descriptionB . length : 0 ) ;
387+
388+ if ( labelDescriptionALength !== labelDescriptionBLength ) {
389+ return labelDescriptionALength - labelDescriptionBLength ;
383390 }
384391
392+ // check for path length and prefer shorter
385393 const pathA = accessor . getItemPath ( itemA ) ;
386394 const pathB = accessor . getItemPath ( itemB ) ;
387395
388396 if ( pathA && pathB && pathA . length !== pathB . length ) {
389- return pathA . length - pathB . length ; // prefer shorter paths
397+ return pathA . length - pathB . length ;
398+ }
399+
400+ // 7.) finally we have equal scores and equal length, we fallback to comparer
401+
402+ // compare by label
403+ if ( labelA !== labelB ) {
404+ return compareAnything ( labelA , labelB , query ) ;
405+ }
406+
407+ // compare by description
408+ if ( descriptionA && descriptionB && descriptionA !== descriptionB ) {
409+ return compareAnything ( descriptionA , descriptionB , query ) ;
390410 }
391411
392- if ( labelA === labelB && pathA && pathB ) {
393- return compareAnything ( pathA , pathB , query ) ; // compare paths if labels are identical
412+ // compare by path
413+ if ( pathA && pathB && pathA !== pathB ) {
414+ return compareAnything ( pathA , pathB , query ) ;
394415 }
395416
396- return compareAnything ( labelA , labelB , query ) ; // finally compare by labels
417+ // equal
418+ return 0 ;
397419}
0 commit comments