Find object inside arraylists by object attribute

  • Thread starter Thread starter Arjen
  • Start date Start date
A

Arjen

Hi,

I have a class with some attributes. For example class person with name as
attribute.
I have add multiple persons in an arraylist.

Now I need a function to get/find a person by the name attribute. This
function returns the index or the object.

I can do this with a foreach statement. I think there are better solutions,
like the binarysearch function inside the arraylist. The problem is that it
can only search persons by objectkey. I need to search by the attribute of
that object.

Can somebody tell me how to do this with a good example?
I found things (google) about IComparer... but I don't understand these
sample's and I don't get is to work.

After that I also want to sort the persons inside the arraylist by an
attribute.
How does this work?

Many thanks!!!
Arjen
 
I can do this with a foreach statement. I think there are better solutions,
like the binarysearch function inside the arraylist.

BinarySearch can only be used if the list is sorted by Name. Otherwise
a linear search (with foreach) is as good as it gets when using an
ArrayList. There are of course other data structures you can use if
you want fast lookup by name, for example a Hashtable keyed by the
name.



Mattias
 
Hi Arjen,
you will need to override the Equals method inside your class such that it
checks for equality based on the attribute you desire, such as name.

For example, i fyou want to check for an item in the arraylist based on the
name of a person:

class clsPerson
{
private string m_strName;

public clsPerson(string strName)
{
m_strName = strName;
}

public string Name
{
get
{
return m_strName;
}
}

//override this to check how two objects are equal
public override bool Equals(Object o)
{
return (clsPerson)o.Name == this.m_strName;
}

//whenever you override equals you should override GetHashCode too
//because some classes will call GetHashCode first before Equals as a
quick
//check for equality, i.e. if the HashCodes match then check equals
otherwise
//keep moving

//The ArrayList.IndexOf method does not call GetHashCode
public override int GetHashCode()
{
return valid hashcode for your object
}
}


Then:
ArrayList lst = new arrayList();
clsPerson p1 = new clsPerson("mark");
clsPerson p2 = new clsPerson("bob");

//this i sthe person we are searching for:
clsPerson p3 = new clsPerson("bob");

//add mark and bob
lst.Add(p1);
lst.Add(p2);

//now does mark exists in the list:
lst.IndexOf(p3) -> should return 1, which is the index location of bob in
the list



Alernatively if you don't want to override the Equals on your object
clsPerson, because that equality test is true for this arraylist situation,
but it may not be for other situations, you could do it on some proxy object
instead that you just use that in your arraylit instead:

i.e.

class PersonSearchProxy
{
private clsPerson m_person;

public PersonSearchProxy(clsPerson p)
{
m_person = p;
}

public clsPerson Value
{
get
{
return m_person;
}
}

//override the equals and gethashcode of this class
public override bool Equals(Object o)
{
return (clsPerson)o.Name == this.m_person.Name;
}

public override int GetHashCode()
{
//return valid hashcode
}
}

//now add instances of PersonSearchProxy to the arrylist.


Hope that helps:
Mark R. Dawson
 
Arjen said:
If I want to use "indexOf" then it uses the "public override bool Equals"
method?

Yes - as per the docs: "This method determines equality by calling
Object.Equals."

It's a shame you can't pass in an IComparer, to allow you to match by
varying criteria.
 

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