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

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance




"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?


.



Relevant Pages

  • Is it possible to define two separate "Sort" methods in the same class?
    ... ArrayList mBuyLimits = new ArrayList; ... ArrayList mSellLimits = new ArrayList; ... public LongTermLimit(double price, int shares) ... I want to sort it by "Price" in descending order. ...
    (microsoft.public.dotnet.general)
  • Re: new kenwood?
    ... NOT microprocessor based, HAM BANDS ONLY... ... command a premium price, and after a couple of years, the price goes down to ... synthesizer/PLL system of some sort. ... Mass-production consumer electronics, most commercial comms ...
    (rec.radio.amateur.equipment)
  • RE: Multiple TSO logons (was: Patents, ...)
    ... Is HOD anything of the sort? ... address space server that provided a GUI interface for most of ... for TSO because among other things, it didn't do native TSO things like ... feedback from a bunch of customers for that price point. ...
    (bit.listserv.ibm-main)
  • Re: racing Sat
    ... Opportunities abound at all the meetings,so there should be something ... Just thought I'd toss it in the pot anyway, as a sort of Saturday *Big ... Here`s one at a price that Ron can get on for the Remy LOL ...
    (uk.sport.horseracing)
  • Re: VHS VCR Question
    ... [Then I put a longish bit about card versus HDD recording as the two future ... to start with a little more on (sort of) the card-as-VCR theme: ... thus perhaps not such a price). ... on whatever the price - it means you can at least change channels when your ...
    (uk.tech.broadcast)