|
-
Jul 4th, 2007, 04:06 AM
#1
Thread Starter
PowerPoster
IComparable
I've implemented this before a very long time ago and since I forgot, i read up about it again to see what the method should look like for CompareTo()
I have implemented this correctly but when I sort the ArrayList giving it null as a parameter, it seems not to sort it but do it in decending order.
So:
I have an arraylist of specific objects (note, this is not a general list....this is using .NET 1.1)
I have implemented the IComparable interface and its method for the object type in question.
I did an: collection.Sort(null); and it didnt seem to sort it (but it does call the compareTo() method):
Code:
#region IComparable Members
public int CompareTo(object obj)
{
// TODO: Add VacancyInfoHelper.CompareTo implementation
if ((obj is VacancyInfoHelper) == false)
{
return -1;
}
VacancyInfoHelper helper = (VacancyInfoHelper)obj;
return this.rank.CompareTo(helper.rank);
}
#endregion
edit: forgot to mention it is an ASP.NET application and since the private field "rank" is set to 0 by default, would that make much difference? I believe it is so how do I do sorting on an ASP.NET based application?
Last edited by Techno; Jul 4th, 2007 at 06:58 AM.
-
Jul 4th, 2007, 07:07 AM
#2
Thread Starter
PowerPoster
Re: IComparable
solved. Had to implement it in a different way.....
if (helper.score < this.score)
{
return -1;
}
else if (helper.score == this.score)
{
return 0;
}
else
{
return 1;
}
-
Jul 4th, 2007, 07:30 AM
#3
Re: IComparable
It's much easier than that even:
C# Code:
return helper.score.CompareTo(this.score);
If 'score' is type 'int' you could also use:
C# Code:
return helper.score - this.score;
Last edited by jmcilhinney; Jul 4th, 2007 at 07:56 AM.
-
Jul 4th, 2007, 12:43 PM
#4
Thread Starter
PowerPoster
Re: IComparable
I tried that but it didn't work - the way I posted after seemed to work correctly and it is a type int
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
|