关于IComparer和Comparer [英] about IComparer and Comparer

查看:56
本文介绍了关于IComparer和Comparer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!


我正在书中读书,这说不是正确的。

传递给Comparer的对象检查.Compare()是否支持

IComparable。

如果他们这样做,则使用该实现


假设我在一个ArrayList中有一个Item的集合

这个名为Item的类支持IComparable接口,它包含

CompareTo方法

所以这个方法在类Item中被强制化了。


现在我可以按照这个CompareTo的实现方式进行排序。


现在假设我希望能够以另一种方式对项目集合进行排序

方式然后这个CompareTo实现了

所以我创建了一个实现IComparer接口的辅助类

包含方法比较。


因此,当我将类型Item的对象传递给比较

方法时,我的问题是

是使用d当然不是IComparable的实现。


// Tony

解决方案

ArrayList。排序有几个重载;无参数的使用

Comparer.Default,(如你所讨论的)将使用对象

IComparable支持(如果有的话)。如果你想要一个自定义排序,有一个

重载(实际上是2)接受你的自定义IComparer,其中

情况Comparer.Default是*不*使用。


请注意,在.NET 2.0中,最好考虑List< T>,

IComparer< T> ;,IComparable< Tand Comparer< T> 。默认。


Marc


6月17日上午9:18,Tony < johansson.anders ... @ telia.comwrote:


我正在书中读书,这说不是正确的。

"传递给Comparer.Compare()的对象被检查以查看它们是否支持

IComparable。

如果他们这样做,则使用该实现;



有一点需要注意的是,大部分时间你应该使用

通用Comparer< Tclass,特别是Comparer< T> ; .Default。


假设我在ArrayList中有一个Item的集合

这个名为Item的类支持IComparable接口包含

CompareTo方法

所以这个方法在类Item中被强制化。


现在我可以按照这个方式排序CompareTo已实现。


现在假设我希望能够以另一种方式对项目集合进行排序

方式然后这个CompareTo实现了/>
所以我创建了一个实现IComparer接口的辅助类,其中
包含方法Compare。



到目前为止一直很好 - 但是那时你不会使用Comparer类,

你只需要传递一个你的实例无论你使用什么排序方法,IComparer实现到



所以当我将类型Item的对象传递给比较

方法实现

当然不是IComparable的实现。



你给出的报价指定了

Comparer.Compare的实现部分。如果您没有调用Comparer.Compare,则

引用无关紧要。如果你想要

来使用自定义IComparer实现,你为什么要调用Comparer.Compare?


如果你提供一个简短的但可能会有所帮助完整的例子,

并询问行为是什么以及特定方法调用的原因。

Jon


这里我没有完整的程序足以回答我的问题。

这里我再次传递一个对象到这个Comparer.Compare()虽然有一个

默认介于两者之间。


>检查传递给Comparer.Compare()的对象以查看它们是否支持

IComparable 。

如果他们这样做,则使用该实现



当我将对象传递给此Comparer.Default.Compare时,我不会使用实现

IComparable上面的文字声称我使用了我想要的任何相关代码。


所以文本声称必须完全不公开。!


公共课项目: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 =值; }

get {return listDateTime; } b / b $

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; }

得到{return steelGrade; }

}


public int HeatNumber

{

set {heatNumber = value; }

得到{return heatNumber; }

}


公共字符串MspName

{

set {mspName = value; }

get {return mspName; }

}


public int CompareTo(对象权限)

{

if(right is项目)

{

Item item = right as Item;

if(this.HeatNumber!= item.HeatNumber)

返回this.HeatNumber.CompareTo(item.HeatNumber);

else

返回this.MspName.CompareTo(item.MspName);

}

else

抛出新的ArgumentException(要比较的对象不是Item

对象);
< br $>
}

}

公共类ItemComparer:IComparer

{

public static IComparer默认= new ItemComparer();


public int比较(对象左,对象右)

{

if(left is Item&& right是Item)

{

if(((Item)left).Selelgrade!=((Item)right).Selelgrade)

返回Comparer.Default.Compare(

((Item)left).Steelgrade,((Item)right).Steelgrade);

else

返回Comparer .Default.Compare(

((Item)left).MspName,((Item)right).MspName);

}

else

抛出新的ArgumentException("其中一个对象不是

Item");

}

}


// Tony

" Jon Skeet [C#MVP]" < sk *** @ pobox.comskrev i meddelandet

新闻:cc *************************** ******* @ 27g2000h sf.googlegroups.com ...


6月17日上午9:18,Tony < johansson.anders ... @ telia.comwrote:


我正在书中读书,这说不是正确的。

"传递给Comparer.Compare()的对象被检查以查看它们是否支持

IComparable。

如果他们这样做,则使用该实现;



有一点需要注意的是,大部分时间你应该使用

通用Comparer< Tclass,特别是Comparer< T> ; .Default。


假设我在ArrayList中有一个Item的集合

这个名为Item的类支持IComparable接口包含





CompareTo method

所以这个方法在类Item中是必需的。


现在我可以按照这个CompareTo的实现方式进行排序。


现在假设我希望能够在
中对项目集合进行排序



另一个


然后这个CompareTo实现了

所以我创建了一个帮助类来实现IComparer接口



其中


包含方法比较。



到目前为止一切都那么好 - 但是那时候你没有使用Comparer课程,

你只是传递了一个你的实例无论你使用什么排序方法,IComparer实现到



所以当我将类型Item的对象传递给比较

方法实现

当然不是IComparable的实现。



你给出的报价指定了

Comparer.Compare的实现部分。如果您没有调用Comparer.Compare,则

引用无关紧要。如果你想要

来使用自定义IComparer实现,你为什么要调用Comparer.Compare?


如果你提供一个简短的但可能会有所帮助完整的例子,

并询问行为是什么以及特定方法调用的原因。

Jon



Hello!

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"

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 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.

//Tony

解决方案

ArrayList.Sort has several overloads; the parameterless one uses
Comparer.Default, which (as you discussed) will use the objects
IComparable support (if any). If you want a custom sort, there is an
overload (2, actually) that accepts your custom IComparer, in which
case Comparer.Default is *not* used.

Note that in .NET 2.0, it would be better to consider List<T>,
IComparer<T>, IComparable<Tand Comparer<T>.Default.

Marc


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


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



这篇关于关于IComparer和Comparer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆