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 - Is it possible to define two separate "Sort" methods in the sameclass?

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


 
 

Is it possible to define two separate "Sort" methods in the sameclass?

I have two ArrayLists:

ArrayList mBuyLimits = new ArrayList();
ArrayList mSellLimits = new ArrayList();

Each item on mBuyLimits and mSellLimits is an instance of
"LongTermLimit" that is defined as below:

public class LongTermLimit
{
protected double mPrice;
protected int mShares;

public LongTermLimit(double price, int shares)
{
mPrice = price;
mShares = shares;
}

public double Price
{
get { return mPrice; }
}

public int Shares
{
set { mShares = value; }
get { return mShares; }
}

}

For mBuyLimits, I want to sort it by "Price" in descending order. For
instance, if mBuyLimits contains the following:

Shares Price
---------------
300 25.04
230 26.04
470 26.6

I want to sort it to:

Shares Price
---------------
470 26.6
230 26.04
300 25.04

For mSellLimits, I want to sort it by "Price" in ascending order. For
instance, if mSellLimits contains the following:

Shares Price
---------------
300 49.58
230 46.2
100 36.04
130 39.42
170 42.81

I want to sort it to:

Shares Price
---------------
100 36.04
130 39.42
170 42.81
230 46.2
300 49.58

Is there a way to define two separate "Sort" methods in the class,
"LongTermLimit". One is for sorting by "Price" in descending order
(for sorting mBuyLimits), and the other one is for sorting by "Price"
in ascending order (for sorting mSellLimits)?

My System SpecsSystem Spec
Old 08-14-2009   #2 (permalink)
PvdG42


 
 

Re: Is it possible to define two separate "Sort" methods in the same class?


"Curious" <fir5tsight@xxxxxx> wrote in message
news:11bd8a8d-8c31-400f-9f87-1ecc9861ae87@xxxxxx
Quote:

> I have two ArrayLists:
>
> ArrayList mBuyLimits = new ArrayList();
> ArrayList mSellLimits = new ArrayList();
>
> Each item on mBuyLimits and mSellLimits is an instance of
> "LongTermLimit" that is defined as below:
>
> public class LongTermLimit
> {
> protected double mPrice;
> protected int mShares;
>
> public LongTermLimit(double price, int shares)
> {
> mPrice = price;
> mShares = shares;
> }
>
> public double Price
> {
> get { return mPrice; }
> }
>
> public int Shares
> {
> set { mShares = value; }
> get { return mShares; }
> }
>
> }
>
> For mBuyLimits, I want to sort it by "Price" in descending order. For
> instance, if mBuyLimits contains the following:
>
> Shares Price
> ---------------
> 300 25.04
> 230 26.04
> 470 26.6
>
> I want to sort it to:
>
> Shares Price
> ---------------
> 470 26.6
> 230 26.04
> 300 25.04
>
> For mSellLimits, I want to sort it by "Price" in ascending order. For
> instance, if mSellLimits contains the following:
>
> Shares Price
> ---------------
> 300 49.58
> 230 46.2
> 100 36.04
> 130 39.42
> 170 42.81
>
> I want to sort it to:
>
> Shares Price
> ---------------
> 100 36.04
> 130 39.42
> 170 42.81
> 230 46.2
> 300 49.58
>
> Is there a way to define two separate "Sort" methods in the class,
> "LongTermLimit". One is for sorting by "Price" in descending order
> (for sorting mBuyLimits), and the other one is for sorting by "Price"
> in ascending order (for sorting mSellLimits)?
The basic rule for methods defined in a class is that each method must be
unique by signature.
A method signature is the method name, plus the parameter list (parameter
type(s), number of parameters, order in which parameters are listed in the
method header). So you can define two methods named Sort in your class as
long as the parameter lists differ. You could write a single Sort method
that takes two arguments, an ArrayList and a flag to indicate ascending or
descending. You could also write two methods, but as it would appear that
the only parameter defined for each would be type ArrayList, the method
names would have to be different.

Here's another design consideration. As you don't define any collection as a
data member in your LongTermLimit class, why would you want to put a Sort
method in that class?


My System SpecsSystem Spec
Old 08-14-2009   #3 (permalink)
Curious


 
 

Re: Is it possible to define two separate "Sort" methods in the sameclass?

On Aug 14, 12:50*pm, "PvdG42" <p...@xxxxxx> wrote:
Quote:

> "Curious" <fir5tsi...@xxxxxx> wrote in message
>
> news:11bd8a8d-8c31-400f-9f87-1ecc9861ae87@xxxxxx
>
>
>
>
>
Quote:

> > I have two ArrayLists:
>
Quote:

> > * * * *ArrayList mBuyLimits = new ArrayList();
> > * * * *ArrayList mSellLimits = new ArrayList();
>
Quote:

> > Each item on mBuyLimits and mSellLimits is an instance of
> > "LongTermLimit" that is defined as below:
>
Quote:

> > * *public class LongTermLimit
> > * *{
> > * * * *protected double mPrice;
> > * * * *protected int mShares;
>
Quote:

> > * * * *public LongTermLimit(double price, int shares)
> > * * * *{
> > * * * * * *mPrice = price;
> > * * * * * *mShares = shares;
> > * * * *}
>
Quote:

> > * * * *public double Price
> > * * * *{
> > * * * * * *get { return mPrice; }
> > * * * *}
>
Quote:

> > * * * *public int Shares
> > * * * *{
> > * * * * * *set { mShares = value; }
> > * * * * * *get { return mShares; }
> > * * * *}
>
Quote:

> > * *}
>
Quote:

> > For mBuyLimits, I want to sort it by "Price" in descending order. For
> > instance, if mBuyLimits contains the following:
>
Quote:

> > Shares Price
> > ---------------
> > 300 25.04
> > 230 26.04
> > 470 26.6
>
Quote:

> > I want to sort it to:
>
Quote:

> > Shares Price
> > ---------------
> > 470 26.6
> > 230 26.04
> > 300 25.04
>
Quote:

> > For mSellLimits, I want to sort it by "Price" in ascending order. For
> > instance, if mSellLimits contains the following:
>
Quote:

> > Shares Price
> > ---------------
> > 300 49.58
> > 230 46.2
> > 100 36.04
> > 130 39.42
> > 170 42.81
>
Quote:

> > I want to sort it to:
>
Quote:

> > Shares Price
> > ---------------
> > 100 36.04
> > 130 39.42
> > 170 42.81
> > 230 46.2
> > 300 49.58
>
Quote:

> > Is there a way to define two separate "Sort" methods in the class,
> > "LongTermLimit". One is for sorting by "Price" in descending order
> > (for sorting mBuyLimits), and the other one is for sorting by "Price"
> > in ascending order (for sorting mSellLimits)?
>
> The basic rule for methods defined in a class is that each method must be
> unique by signature.
> A method signature is the method name, plus the parameter list (parameter
> type(s), number of parameters, order in which parameters are listed in the
> method header). So you can define two methods named Sort in your class as
> long as the parameter lists differ. You could write a single Sort method
> that takes two arguments, an ArrayList and a flag to indicate ascending or
> descending. You could also write two methods, but as it would appear that
> the only parameter defined for each would be type ArrayList, the method
> names would have to be different.
>
> Here's another design consideration. As you don't define any collection as a
> data member in your LongTermLimit class, why would you want to put a Sort
> method in that class?- Hide quoted text -
>
> - Show quoted text -
I want to use IComparable and define the built-in Sort method. The
collection is mBuyLimits/mSellLimits.
My System SpecsSystem Spec
Old 08-14-2009   #4 (permalink)
Curious


 
 

Re: Is it possible to define two separate "Sort" methods in the sameclass?

I apologize. I meant "CompareTo" method, not "Sort".

I'll need to define my class as below:

public class LongTermLimit : IComparable
{
public int CompareTo(object obj)
{
//bla bla
}
}

I don't know how to define two separate CompareTo methods, one for the
descending sort, and the other for ascending sort.

My System SpecsSystem Spec
Old 08-14-2009   #5 (permalink)
Family Tree Mike


 
 

Re: Is it possible to define two separate "Sort" methods in the sameclass?

Curious wrote:
Quote:

> I apologize. I meant "CompareTo" method, not "Sort".
>
> I'll need to define my class as below:
>
> public class LongTermLimit : IComparable
> {
> public int CompareTo(object obj)
> {
> //bla bla
> }
> }
>
> I don't know how to define two separate CompareTo methods, one for the
> descending sort, and the other for ascending sort.
>
Set a property in your class that the CompareTo method uses to determine
the sort order within the single method. Set the property depending on
the requirement at the time of call.

--
Mike
My System SpecsSystem Spec
Old 08-14-2009   #6 (permalink)
Curious


 
 

Re: Is it possible to define two separate "Sort" methods in the sameclass?

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

> Curious wrote:
Quote:

> > I apologize. I meant "CompareTo" method, not "Sort".
>
Quote:

> > I'll need to define my class as below:
>
Quote:

> > * *public class LongTermLimit : IComparable
> > * * {
> > * * * * * * public int CompareTo(object obj)
> > * * * * * * {
> > * * * * * * * * //bla bla
> > * * * * * * }
> > * * }
>
Quote:

> > I don't know how to define two separate CompareTo methods, one for the
> > descending sort, and the other for ascending sort.
>
> Set a property in your class that the CompareTo method uses to determine
> the sort order within the single method. *Set the property depending on
> the requirement at the time of call.
>
> --
> Mike- Hide quoted text -
>
> - Show quoted text -
Thanks for the idea!
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Use "lock" in three methods to prevent shared-memory corruption .NET General
When do I have do define variables with "DIM" ? VB Script
In Outlook Express, how can I define the "columns" other than the "InBox" setting Live Mail
Search - Detail View - "Folder" is backwards - can't sort properly Vista file management
Separate hashtable vs $alist | where-object { "key" = "value" } versus something else? PowerShell


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