Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement incremental search on a list

Tags:

c#

.net

search

I want to implement incremental search on a list of strings. Consider I have an array containing which contains the strings store,state,stamp,crawl,crow. My application has a text box in which the user enters the search string. Now, as the user enters the text, I need to highlight all the matches. For example, when the user enters "st" I need to highlight "Store,state,stamp" now when he types "a", I need to remove "Store" from the list.I am developing the application using c# with .net framework. What I plan to do is , on event on which text changes, I do a search in background and show the results. Is there any other way to solve this ?

like image 968
Pi. Avatar asked Mar 18 '09 13:03

Pi.


1 Answers

You could just look at the newly entered letter; if the new third letter is an 'a' just throw out all elements without 'a' at position three. If the user deletes a letter you have to rescan the whole original list and bring back all priviously removed items.

But what if the user pastes multiple letters from the clipboard, deletes multiple letters by selecting them, inserts or deletes a single or multiple letters somewhere in the middle?

You have just to many cases to watch for. You could do the method with the newly entered letter an fall back to a complete rescan if the search text changed in a way other than adding a single letter, but even this simple method is probably not worth the effort just to avoid a few ten or hundred string comparisons. As already mentioned, a Trie or Patricia trie is the way to go if you have really large data sets or want to be really quick.

like image 117
Daniel Brückner Avatar answered Oct 15 '22 02:10

Daniel Brückner