ArrayList Search for Value

  • Thread starter Thread starter John Veldthuis
  • Start date Start date
J

John Veldthuis

I have an ArrayList set up with a set of class objects.

The class has 3 strings in it and are as follows

ProdID
GSP
Description

Okay now problem with getting all these into the list and displaying them correctly and manipulating
things except for one thing.

I wish to be able to do search through the whole list without stepping through the entire list
enumerating them. I would just like to be able to search on one field. The ProdID.

For now I do it by

dim myspec as new clsSpecs
for each myspec in specList
if test.prodid.compareto(myspec.ProdID) = 0 then

do stuff

end if

next


Now this works but I am looking for a search method but list.BinarySearch does not seem to do it.

Any help?
 
John Veldthuis said:
I have an ArrayList set up with a set of class objects.

The class has 3 strings in it and are as follows

ProdID
GSP
Description

Okay now problem with getting all these into the list and displaying them
correctly and manipulating
things except for one thing.

I wish to be able to do search through the whole list without stepping
through the entire list
enumerating them. I would just like to be able to search on one field. The
ProdID.

For now I do it by

dim myspec as new clsSpecs
for each myspec in specList
if test.prodid.compareto(myspec.ProdID) = 0 then

do stuff

end if

next


Now this works but I am looking for a search method but list.BinarySearch
does not seem to do it.

Any help?

I suggest you read this article:

http://msdn.microsoft.com/library/d...llectionsarraylistclassbinarysearchtopic1.asp

You'll note that the objects in your ArrayList must come from a class that
implements the IComparable interface, and that the objects must be sorted
according to your IComparable implementation. Then the BinarySearch method
will yield the results you seek.
 
Thanks. I had implemented IComparable to sort the list but was using it on the GSP entry.
I changed it to look at the ProdID and it did return what I expected. Changed it back as that was
what I needed the main sort on.

However I added an IComparable called FindProdID and used the following

myspec = new clsSpecs
mySpec.ProdID = "100676"
indx = specList.BinarySearch(myspec, new FindProdID)

and all worked well and returned the index it should have.

Thanks for the pointer.
 
John,

If you are sure that you want to use the complete word in the exact cases,
than you can use the arraylist indexof method

http://msdn.microsoft.com/library/d...stemcollectionsarraylistclassindexoftopic.asp

If you want it undependable from the cases than you should be able to use
the Icomparer as nicely described by Peter Proost, Jay B. Harlow and more
people in this newsgroup this week

http://groups.google.com/group/micr...7c859a13cd0/639adf0d52e87dfc#639adf0d52e87dfc

If you are the same John, than forget this message.

I hope this helps,

Cor
 
Don't you have to sort the arraylist on the ProdID property first before
using the binarysearch?
 
Don't you have to sort the arraylist on the ProdID property first before
using the binarysearch?

Yes, I simply resorted using the new IComparer and then did the search and then resorted back the
original.
 
John,

If you are sure that you want to use the complete word in the exact cases,
than you can use the arraylist indexof method

http://msdn.microsoft.com/library/d...stemcollectionsarraylistclassindexoftopic.asp

If you want it undependable from the cases than you should be able to use
the Icomparer as nicely described by Peter Proost, Jay B. Harlow and more
people in this newsgroup this week

http://groups.google.com/group/micr...7c859a13cd0/639adf0d52e87dfc#639adf0d52e87dfc

If you are the same John, than forget this message.

I hope this helps,

Thanks, different John but I understood all that. My problem was that I did not just have a simple
entry as the example but a class and was unsure of how the binarysearch worked on only filling out
part of the class. However all fixed now and working 100% how I expected thanks to the help and
pointers from those here.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top