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

  • Thread starter Thread starter Curious
  • Start date Start date
C

Curious

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)?
 
Curious said:
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?
 
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.
 
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.
 
Curious said:
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.
 
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.

Thanks for the idea!
 
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.

You could define two different IComparer classes that could be passed
to the Sort method.
 
Back
Top