Searching Display Data with BinarySearches

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have implemented custom sorting and searches on my complex objects (they
contain properties and collections) using IComparer and BinarySearch. I knew
they before doing a search you had to sort your collection, but I found out
the hard way yesterday that it has to be sorted on the same property as you
are searching on. Fine. But now my problem is that while displaying this
collection data to the user I have to search on a different property then the
collection is currently sorted on.

For example, the user has chosen to sort the data on PropertyA. An event
happens and I need to find an item in the collection based on PropertyB. It
does not work unless I sort the collection on PropertyB first. If I do this,
then the data the user sees changes. Very bad.

So I guess what I am asking for is a way to search a collection, return the
requested object (index) without disturbing the original data (collection).
Any ideas?

Thanks!
David McCarter

=====================
David McCarter
www.vsdntips.com
 
David,

Well, you don't have to use a binary search in this case. You could
cycle through all of the items in the collection, and just do a straight
comparison against them.

Or, you could create a new collection (where there is nothing bound to
it, like the ui), add the elements to that collection in the proper sorted
order, then perform your binary search.

Hope this helps.
 
Dave.... In the ancient past having written file based data storage, I
look at the problem as data and index. You can have more than one index
for the data. The index simply contains pointers to the actual data
bucket and you can sort and search a given index. So off the top of my
pointed head I would consider creating a separate sorted index. Perhaps
an arraylist that is synchronized with the data. Now this could get
messy but there are ways to obtain a sync lock on a collection, do a
foreach on the locked collection, generating an array list, and then
releasing the lock, sorting and searching the arraylist.

Regards,
Jeff
So I guess what I am asking for is a way to search a collection, return
the requested object (index) without disturbing the original data
(collection).
Any ideas?
 
Back
Top