public int Compare(KeyValuePair <int, string> xpair, KeyValuePair <int, string> ypair) { string x = xpair.Value; string y = ypair.Value; int[] xMatches = ListWidget.Match(filterWord, x) ?? new int[0]; int[] yMatches = ListWidget.Match(filterWord, y) ?? new int[0]; if (xMatches.Length < yMatches.Length) { return(1); } if (xMatches.Length > yMatches.Length) { return(-1); } int xExact = 0; int yExact = 0; for (int i = 0; i < filterWord.Length; i++) { if (i < xMatches.Length && filterWord[i] == x[xMatches[i]]) { xExact++; } if (i < yMatches.Length && filterWord[i] == y[yMatches[i]]) { yExact++; } } if (xExact < yExact) { return(1); } if (xExact > yExact) { return(-1); } // favor words where the match starts sooner if (xMatches.Length > 0 && yMatches.Length > 0 && xMatches[0] != yMatches[0]) { return(xMatches[0].CompareTo(yMatches[0])); } int xIndex = xpair.Key; int yIndex = ypair.Key; if (x.Length == y.Length) { return(xIndex.CompareTo(yIndex)); } return(x.Length.CompareTo(y.Length)); }
protected int FindMatchedEntry(string partialWord, out bool hasMismatches) { // default - word with highest match rating in the list. hasMismatches = true; int idx = -1; List <KeyValuePair <int, string> > words = new List <KeyValuePair <int, string> > (); if (!string.IsNullOrEmpty(partialWord)) { for (int i = 0; i < list.filteredItems.Count; i++) { int index = list.filteredItems[i]; string text = DataProvider.GetCompletionText(index); //Console.WriteLine("partialWord->"+partialWord); //Console.WriteLine("text->"+text); if (!ListWidget.Matches(partialWord, text)) { continue; } words.Add(new KeyValuePair <int, string> (i, text)); } } ListWindow.WordComparer comparer = new WordComparer(partialWord); if (words.Count > 0) { words.Sort(comparer); idx = words[0].Key; hasMismatches = false; // exact match found. if (words[0].Value.ToUpper() == (partialWord ?? "").ToUpper()) { return(idx); } } if (string.IsNullOrEmpty(partialWord) || partialWord.Length <= 2) { // Search for history matches. for (int i = 0; i < wordHistory.Count; i++) { string historyWord = wordHistory[i]; if (ListWidget.Matches(partialWord, historyWord)) { for (int xIndex = 0; xIndex < list.filteredItems.Count; xIndex++) { string currentWord = DataProvider.GetCompletionText(list.filteredItems[xIndex]); if (currentWord == historyWord) { idx = xIndex; break; } } } } } return(idx); }