Results 1 to 4 of 4

Thread: IComparable

Hybrid View

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    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.

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  2. #2

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    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;
    }

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: IComparable

    It's much easier than that even:
    C# Code:
    1. return helper.score.CompareTo(this.score);
    If 'score' is type 'int' you could also use:
    C# Code:
    1. return helper.score - this.score;
    Last edited by jmcilhinney; Jul 4th, 2007 at 07:56 AM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    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

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width