Re: Is it possible to define two separate "Sort" methods in the same class?
- From: "PvdG42" <pvdg@xxxxxxxxxxxxx>
- Date: Fri, 14 Aug 2009 11:50:12 -0500
"Curious" <fir5tsight@xxxxxxxxx> wrote in message news:11bd8a8d-8c31-400f-9f87-1ecc9861ae87@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
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?
.
- Follow-Ups:
- References:
- Prev by Date: LDAP Query to filter disabled account
- Next by Date: Re: Is it possible to define two separate "Sort" methods in the same class?
- Previous by thread: Is it possible to define two separate "Sort" methods in the same class?
- Next by thread: Re: Is it possible to define two separate "Sort" methods in the same class?
- Index(es):
Relevant Pages
|