Windows Vista Forums

What's wrong with "CompareTo" method?
  1. #1


    Curious Guest

    What's wrong with "CompareTo" method?

    I have an ArrayList, mBuyLimits. Each item on mBuyLimits is an
    instance of class "LongTermLimitOnBuy" as defined below:



    public class LongTermLimitOnBuy : LongTermLimit, IComparable
    {
    public LongTermLimitOnBuy(double price, int shares) : base
    (price, shares)
    {
    }

    // Sort by Price in descending order
    public int CompareTo(object other)
    {
    LongTermLimitOnBuy lb = (LongTermLimitOnBuy)other;

    if (this.Price >= lb.Price)
    {
    return 1;
    }
    else
    {
    return 0;
    }
    }
    }

    However, after I execute the following:

    mBuyLimits.Sort();

    The items on mBuyLimits are not sorted by Price in descending order.
    They appear to be in random order. How come they are not sorted?

      My System SpecsSystem Spec

  2. #2


    Family Tree Mike Guest

    Re: What's wrong with "CompareTo" method?

    Curious wrote:

    > I have an ArrayList, mBuyLimits. Each item on mBuyLimits is an
    > instance of class "LongTermLimitOnBuy" as defined below:
    >
    > public class LongTermLimitOnBuy : LongTermLimit, IComparable
    > {
    > public LongTermLimitOnBuy(double price, int shares) : base
    > (price, shares)
    > {
    > }
    >
    > // Sort by Price in descending order
    > public int CompareTo(object other)
    > {
    > LongTermLimitOnBuy lb = (LongTermLimitOnBuy)other;
    >
    > if (this.Price >= lb.Price)
    > {
    > return 1;
    > }
    > else
    > {
    > return 0;
    > }
    > }
    > }
    >
    > However, after I execute the following:
    >
    > mBuyLimits.Sort();
    >
    > The items on mBuyLimits are not sorted by Price in descending order.
    > They appear to be in random order. How come they are not sorted?
    Because you are returning 0, meaning they are equal, only when they are
    not equal. First try returning: this.Price.CompareTo(lb.Price)


    --
    Mike

      My System SpecsSystem Spec

  3. #3


    Curious Guest

    Re: What's wrong with "CompareTo" method?

    On Aug 14, 5:28*pm, Family Tree Mike <FamilyTreeM...@xxxxxx>
    wrote:

    > Curious wrote:

    > > I have an ArrayList, mBuyLimits. Each item on mBuyLimits is an
    > > instance of class "LongTermLimitOnBuy" as defined below:
    >

    > > * * public class LongTermLimitOnBuy : LongTermLimit, IComparable
    > > * * {
    > > * * * * public LongTermLimitOnBuy(double price, int shares) : base
    > > (price, shares)
    > > * * * * {
    > > * * * * }
    >

    > > * * * * // Sort by Price in descending order
    > > * * * * public int CompareTo(object other)
    > > * * * * {
    > > * * * * * * LongTermLimitOnBuy lb = (LongTermLimitOnBuy)other;
    >

    > > * * * * * * if (this.Price >= lb.Price)
    > > * * * * * * {
    > > * * * * * * * * return 1;
    > > * * * * * * }
    > > * * * * * * else
    > > * * * * * * {
    > > * * * * * * * * return 0;
    > > * * * * * * }
    > > * * * * }
    > > * * }
    >

    > > However, after I execute the following:
    >

    > > mBuyLimits.Sort();
    >

    > > The items on mBuyLimits are not sorted by Price in descending order.
    > > They appear to be in random order. How come they are not sorted?
    >
    > Because you are returning 0, meaning they are equal, only when they are
    > not equal. *First try returning: this.Price.CompareTo(lb.Price)
    >
    > --
    > Mike- Hide quoted text -
    >
    > - Show quoted text -
    Family Tree Mike,

    Thanks! it works.

      My System SpecsSystem Spec

What's wrong with "CompareTo" method? problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing "| find" in command executed with EXEC method does not work Steve VB Script 7 05 Aug 2009
"Winston's" Scorched Earth Uninstall/Reinstall Method For Fixing Windows Live Mail [WLM] D. Spencer Hines Live Mail 4 16 Jan 2009
Help wanted: Vistas "Windows Mail" and older SSL encryption method Magnus Flysjö Vista mail 0 17 Oct 2007
IDEA: scriptmethod "method overload" (and functions too) klumsy@xtra.co.nz PowerShell 0 30 Jun 2007
Any method for me to clean "Recent Items" other than deleting them one by one? Elliot Vista General 4 06 Jun 2007