Searching for a particular object in an arraylist

  • Thread starter Thread starter AAJ
  • Start date Start date
A

AAJ

Hi all

I have some objects that I add to an ArrayList. Each object is from the same
class and has methods etc. Each can be identified by a unique property, in
this case PK

e.g.

PK =1 for Instance1.PK
PK =2 for Instance2.PK

etc

I need to be able to locate the index of a specific PK within the Array List
using the .PK property. I know I could use a FOREACH loop, and check each PK
of the Current.PK until I find the correct one.

I'm hoping there is a built in way of doing this. I looked at
ArrayList.BinarySearch, but this seems to compare the full object, but its
the PK that I need to find, and then return the index of the located object.

I can find references to Icomparable, Icompare, I can find references to
finding integers, strings etc, but I cant find searching for a particular
property. I am also aware that for a binary search, the list needs to be
sorted.

can any one point me to a tutorial, perhaps I should be using a hashtable
and storing PK

andy advice would be welcome

thanks in advance

Andy
 
Hi AAJ,
for fast retrieval of information a Hashtable (or Dictionary in 2.0) is
definitely the way to go, since you have Random Access capabilities rather
than employing some search algorithmn.

If you are goign to use your objects in the hashtable as a key then make
sure you have overriden the Equals and GetHashCode method.

Mark Dawson
http://www.markdawson.org
 
Andy,

1. Make your object implement IComparable.
2. In the CompareTo method needed for IComparable, compare the PK properties
3. Sort your ArrayList using the ArrayList.Sort method
4. Use the BinarySearch method to find an object.

Dave
 
For this particular case, I agree that IComparable is the right way to
go.

However, it should be pointed out that IComparable should be implemented
for the natural sort of an object. In other words, it is a way for the
object to indicate the way it should be compared to others of the same type.

If the sort order is different, then an implementation of IComparer
should be provided to the Sort and BinarySearch methods which would evaluate
the comparisons separate from the objects.
 
Thanks all

I have used the hashtable method, but for the experience, I think I'll have
a go with IComparable as well

Andy


Nicholas Paldino said:
For this particular case, I agree that IComparable is the right way to
go.

However, it should be pointed out that IComparable should be
implemented for the natural sort of an object. In other words, it is a
way for the object to indicate the way it should be compared to others of
the same type.

If the sort order is different, then an implementation of IComparer
should be provided to the Sort and BinarySearch methods which would
evaluate the comparisons separate from the objects.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

D. Yates said:
Andy,

1. Make your object implement IComparable.
2. In the CompareTo method needed for IComparable, compare the PK
properties
3. Sort your ArrayList using the ArrayList.Sort method
4. Use the BinarySearch method to find an object.

Dave
 
Just a final note to say thanks

have hashtables, sorting and BinarySearching all working now

thanks All



AAJ said:
Thanks all

I have used the hashtable method, but for the experience, I think I'll
have a go with IComparable as well

Andy


Nicholas Paldino said:
For this particular case, I agree that IComparable is the right way to
go.

However, it should be pointed out that IComparable should be
implemented for the natural sort of an object. In other words, it is a
way for the object to indicate the way it should be compared to others of
the same type.

If the sort order is different, then an implementation of IComparer
should be provided to the Sort and BinarySearch methods which would
evaluate the comparisons separate from the objects.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

D. Yates said:
Andy,

1. Make your object implement IComparable.
2. In the CompareTo method needed for IComparable, compare the PK
properties
3. Sort your ArrayList using the ArrayList.Sort method
4. Use the BinarySearch method to find an object.

Dave


"AAJ" <a.a.com> wrote in message
Hi all

I have some objects that I add to an ArrayList. Each object is from the
same class and has methods etc. Each can be identified by a unique
property, in this case PK

e.g.

PK =1 for Instance1.PK
PK =2 for Instance2.PK

etc

I need to be able to locate the index of a specific PK within the Array
List using the .PK property. I know I could use a FOREACH loop, and
check each PK of the Current.PK until I find the correct one.

I'm hoping there is a built in way of doing this. I looked at
ArrayList.BinarySearch, but this seems to compare the full object, but
its the PK that I need to find, and then return the index of the
located object.

I can find references to Icomparable, Icompare, I can find references
to finding integers, strings etc, but I cant find searching for a
particular property. I am also aware that for a binary search, the list
needs to be sorted.

can any one point me to a tutorial, perhaps I should be using a
hashtable and storing PK

andy advice would be welcome

thanks in advance

Andy
 
Back
Top