|
-
Apr 10th, 2007, 04:11 AM
#1
Thread Starter
PowerPoster
compareTo/Equals
It's been a while since ive done this. Basically I have an object with properties which include:
ID (int)
name (string)
I want to be able to sort the collection based on the name. I have implemented and overriden CompareTo, Equals, GetHashCode and ToString however I have noticed that it shows me much less items in the collection than before overriding these. Any ideas where I am going wrong?
public int CompareTo(GenericListRecip other)
{
return this.name.CompareTo(other.name); //this method does not even get called!
}
public override string ToString()
{
return this.name;
}
public override int GetHashCode()
{
return this.id;
}
public override Equals(object obj)
{
if (obj is GenericListRecip)
{
return this.name == ((GenericListRecip)obj).name;
}
else
{
return false;
}
}
obviously if i take the implementation out - its all fine but doesnt sort it by the property/field I would like. Any ideas?
-
Apr 10th, 2007, 05:30 AM
#2
Re: compareTo/Equals
If you want to be able to sort a list of these objects then you should implement the IComparable interface. It is IComparable.Compare that is used when sorting.
-
Apr 10th, 2007, 07:47 AM
#3
Thread Starter
PowerPoster
Re: compareTo/Equals
Thanks, I did implement that but that didn't quite work however ive managed to make it work now - I had to call the Sort() method excplicitly...seems to work fine, calls the compareto method and works fine :-/
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|