.FindString implementation

  • Thread starter Thread starter Marcin Floryan
  • Start date Start date
M

Marcin Floryan

Hello!

I have recently come across two very nice methods in ListBox control:
..FindString and .FindStringExact
it is very usefull for me, and I would like to use it in some other
situations.

Where could I find an implementation of thiese methods or other similar
solutions. I would like to use such methods without ListBox - only with a
simple
Collection like SortedList (key and value). I have created a simple
implementation myself but I'm not sure it's that quick.

SortedList searchList = new SortedList();

// [...] - fill the list
foreach (DictionaryEntry de in searchList) {

string strKey = (string) de.Key;

if (strKey.StartsWith(strSearched)) {

return de.Value; break;

} else {

if (strKey.CompareTo(strSearched) > 0) break;

}

}


Marcin Floryan
marcin at floryan dot neostrada dot pl
 
Marcin,

This is not as fast as it could be. What I would do is get the keys,
and then perform a binary search on it. Basically, since the keys should be
ordered (it is a sorted list, right), you can basically use a binary search.

Hope this helps.
 
Back
Top