Here I have not Complete program enough to be able to answer my question.
Here again I pass an object to this Comparer.Compare() although there is an
Default in between.
>Objects passed to Comparer.Compare() are checked to see if they support
IComparable.
If they do, then that implementation is used"
Below when I pass object to this Comparer.Default.Compare I don't use the
implementation from
IComparable that the text above is claiming I use whatever relevent code I
want.?
So what the text is claiming must be completely nonsens.!
public class Item : IComparable
{
private int steelGrade;
private int heatNumber;
private string mspName;
private string dateTime;
private int optTime;
private ArrayList listOpt;
private ArrayList listDateTime;
public ArrayList ListDateTime
{
set { listDateTime = value; }
get { return listDateTime; }
}
public ArrayList ListOpt
{
set {listOpt = value; }
get { return listOpt; }
}
public int OptTime
{
set { optTime = value; }
get { return optTime; }
}
public string DateTime
{
set { dateTime = value; }
get { return dateTime; }
}
public int Steelgrade
{
set { steelGrade = value; }
get { return steelGrade; }
}
public int HeatNumber
{
set { heatNumber = value; }
get { return heatNumber; }
}
public string MspName
{
set { mspName = value; }
get { return mspName; }
}
public int CompareTo(object right)
{
if (right is Item)
{
Item item = right as Item;
if (this.HeatNumber != item.HeatNumber)
return this.HeatNumber.CompareTo(item.HeatNumber);
else
return this.MspName.CompareTo(item.MspName);
}
else
throw new ArgumentException("Object to compare is not a Item
object");
}
}
public class ItemComparer : IComparer
{
public static IComparer Default = new ItemComparer();
public int Compare(object left, object right)
{
if (left is Item && right is Item)
{
if (((Item)left).Steelgrade != ((Item)right).Steelgrade)
return Comparer.Default.Compare(
((Item)left).Steelgrade, ((Item)right).Steelgrade);
else
return Comparer.Default.Compare(
((Item)left).MspName, ((Item)right).MspName);
}
else
throw new ArgumentException("One of the object is not an
Item");
}
}
//Tony
"Jon Skeet [C# MVP]" <sk***@pobox.comskrev i meddelandet
news:cc**********************************@27g2000h sf.googlegroups.com...
On Jun 17, 9:18 am, "Tony" <johansson.anders...@telia.comwrote:
I'm reading in a book and this can't be correct it says.
"Objects passed to Comparer.Compare() are checked to see if they support
IComparable.
If they do, then that implementation is used"
One thing to note is that most of the time you should now be using the
generic Comparer<Tclass and in particular Comparer<T>.Default.
Assume I have a collection of Item(s) in an ArrayList
This class called Item support the IComparable interface which contains
the
CompareTo method
so this method is impemented in class Item.
Now I can sort in the way that this CompareTo is implemented.
Now assume I want to be able to sort the collection of item(s) in
another
way then this CompareTo is implementet
so I create an helper class which implement the IComparer interface
which
contains method Compare.
So far so good - but at that point you don't use the Comparer class,
you just pass an instance of your IComparer implementation into
whatever sorting method you're using.
So not to my question when object of type Item is passed to the Compare
method that implementation
is used of course not the implementation that exist for the IComparable.
The quote you gave specified part of the implementation of
Comparer.Compare. If you're not calling Comparer.Compare, then the
quote is irrelevant. Why would you call Comparer.Compare if you want
to use a custom IComparer implementation?
Perhaps it would help if you'd provide a short but complete example,
and ask what the behaviour is and why for a specific method call.
Jon