![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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? not equal. First try returning: this.Price.CompareTo(lb.Price) -- Mike |
My System Specs![]() |
| | #3 (permalink) |
| | 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 - Thanks! it works. |
My System Specs![]() |
![]() |
| 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 | |||