Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > .NET General

Vista - What's wrong with "CompareTo" method?

Reply
 
Old 08-14-2009   #1 (permalink)
Curious


 
 

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
Old 08-14-2009   #2 (permalink)
Family Tree Mike


 
 

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

Curious wrote:
Quote:

> 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
Old 08-17-2009   #3 (permalink)
Curious


 
 

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

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

> Curious wrote:
Quote:

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

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

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

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

> > However, after I execute the following:
>
Quote:

> > mBuyLimits.Sort();
>
Quote:

> > 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
Reply

Thread Tools


Similar Threads
Thread Forum
Passing "| find" in command executed with EXEC method does not work VB Script
"Winston's" Scorched Earth Uninstall/Reinstall Method For Fixing Windows Live Mail [WLM] Live Mail
Help wanted: Vistas "Windows Mail" and older SSL encryption method Vista mail
IDEA: scriptmethod "method overload" (and functions too) PowerShell
Any method for me to clean "Recent Items" other than deleting them one by one? Vista General


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46