C# 如何将多个对象值相互比较?
假设我有一个对象,其中包含以下值(请注意,我不希望为此使用datetime对象,仅使用以下值,我希望在比较器本身中解决此问题): 如何将比较器中的所有这些多个值相互比较,以便根据日期列出它们 然后我想制作一个Comparer.cs类:C# 如何将多个对象值相互比较?,c#,icomparer,C#,Icomparer,假设我有一个对象,其中包含以下值(请注意,我不希望为此使用datetime对象,仅使用以下值,我希望在比较器本身中解决此问题): 如何将比较器中的所有这些多个值相互比较,以便根据日期列出它们 然后我想制作一个Comparer.cs类: class MyComparer: IComparer { int sort; public MyComparer(int s) { sort= s; } public int Compare(objec
class MyComparer: IComparer
{
int sort;
public MyComparer(int s)
{
sort= s;
}
public int Compare(object x, object y)
{
Date d1 = (Date)x;
Date d2 = (Date)y;
int result= 0;
// d1.Year.CompareTo(d2.Year); //get accessors from other class
// i seem to be limited here by comparing only 1 single value to a other?
return result;
}
}
}你只需要不断地进行比较,直到你有了不同或者没有什么东西可以比较为止
int result;
result = d1.year.CompareTo(d2.year);
if (result == 0)
result = d1.month.CompareTo(d2.month);
if (result == 0)
// continue comparisons through day, hour, minute, second as necessary
return result;
像这样:
int result = d1.Year.CompareTo(d2.Year);
if (result != 0) return result;
result = d1.Month.CompareTo(d2.Month);
if (result != 0) return result;
...
return 0; //All properties were equal
首先比较最重要的元素,如下所示:
public class MyDate : IComparable<MyDate>
{
public enum MyMonth { Jan = 1 , Feb , Mar , Apr , May , Jun , Jul , Aug , Sep , Oct , Nov , Dec , }
public int Year ;
public MyMonth Month ;
public int Day ;
public int Hour ;
public int Minute ;
public int Second ;
private static Comparer _comparerInstance = null ;
private static Comparer comparerInstance
{
get
{
if ( _comparerInstance == null )
{
_comparerInstance = new Comparer() ;
}
return _comparerInstance ;
}
}
public class Comparer : IComparer<MyDate>
{
#region IComparer<MyDate> Members
public int Compare(MyDate x, MyDate y)
{
if ( x == null || y == null ) throw new ArgumentNullException() ;
if ( object.ReferenceEquals(x,y) ) return 0 ;
int rc ;
if ( x.Year < y.Year ) rc = -1 ;
else if ( x.Year > y.Year ) rc = +1 ;
else // x.Year == y.Year
{
if ( x.Month < y.Month ) rc = -1 ;
else if ( x.Month > y.Month ) rc = +1 ;
else // x.Month == y.Month
{
if ( x.Day < y.Day ) rc = -1 ;
else if ( x.Day > y.Day ) rc = +1 ;
else // x.Day == y.Day
{
if ( x.Hour < y.Hour ) rc = -1 ;
else if ( x.Hour > y.Hour ) rc = +1 ;
else // x.Hour == y.Hour
{
if ( x.Minute < y.Minute ) rc = -1 ;
else if ( x.Minute > y.Minute ) rc = +1 ;
else // x.Minute = y.Minute
{
if ( x.Second < y.Second ) rc = -1 ;
else if ( x.Second > y.Second ) rc = -1 ;
else /* x.Second == y.Seconds */ rc = 0 ;
}
}
}
}
}
return rc ;
}
#endregion
}
#region IComparable<MyDate> Members
public int CompareTo( MyDate other )
{
return comparerInstance.Compare( this , other ) ;
}
#endregion
}
公共类MyDate:IComparable
{
公共枚举MyMonth{Jan=1,二月,三月,四月,五月,六月,七月,八月,九月,十月,十一月,十二月,}
公共国际年;
公共月;
公众国际日;
公共时间;
公共分钟;
第二次公开辩论;
私有静态比较器_comparerInstance=null;
专用静态比较器比较器状态
{
得到
{
if(_compareInstance==null)
{
_comparerInstance=新比较器();
}
返回-比较状态;
}
}
公共类比较器:IComparer
{
#地区I比较成员
公共整数比较(MyDate x,MyDate y)
{
如果(x==null | | y==null)抛出新的ArgumentNullException();
if(object.ReferenceEquals(x,y))返回0;
int rc;
如果(x年y年)rc=+1;
else//x.年==y.年
{
如果(x月y月)rc=+1,则为else;
else//x.月==y.月
{
如果(x日y日)rc=+1;
否则//x.日==y.日
{
如果(x小时y.小时)rc=+1;
else//x.小时==y.小时
{
如果(x.分钟y.分钟)rc=+1;
else//x.分钟=y.分钟
{
如果(x.秒y秒)rc=-1;
else/*x.Second==y.Seconds*/rc=0;
}
}
}
}
}
返回rc;
}
#端区
}
#区域i可比较成员
公共整数比较(MyDate其他)
{
返回compareInstance.Compare(这个,其他);
}
#端区
}
这只是无聊的管道工程,没有什么神奇之处。这很容易做到,所以请告诉我们您在这里到底不明白什么。还有一个小提示:使用IComparer。您还可以让
Date
类实现IComparable
接口,这样您就不必强制转换对象。或者您可以让您的MyComparer
实现IComparer
界面。不同之处在于
的类型正确,所以您肯定拥有正确的对象。@Al-Kepp:对不起,双音符=)我已经写下了。谢谢,我如何在我的另一个类中使用它?如果我想在那里声明CompareTo方法,请返回这个。但是我的意思是,我不想把一年或者一分钟的时间和整个负载进行比较。我怎样才能把它写下来?问候。和我写答案的方式一样,但与打电话的方式不同。你知道我的代码是如何工作的吗?几乎可以,但是当它不相等并且返回结果时会发生什么呢?在我的另一节课上,我被困在如何调用这个。我只是比较单个值,没有问题,但现在我很困惑,尽管我希望正确理解这一点。致以最诚挚的问候。如果result
为非零,则表示您找到了不匹配的属性。因此,比较结果取决于该属性。
public class MyDate : IComparable<MyDate>
{
public enum MyMonth { Jan = 1 , Feb , Mar , Apr , May , Jun , Jul , Aug , Sep , Oct , Nov , Dec , }
public int Year ;
public MyMonth Month ;
public int Day ;
public int Hour ;
public int Minute ;
public int Second ;
private static Comparer _comparerInstance = null ;
private static Comparer comparerInstance
{
get
{
if ( _comparerInstance == null )
{
_comparerInstance = new Comparer() ;
}
return _comparerInstance ;
}
}
public class Comparer : IComparer<MyDate>
{
#region IComparer<MyDate> Members
public int Compare(MyDate x, MyDate y)
{
if ( x == null || y == null ) throw new ArgumentNullException() ;
if ( object.ReferenceEquals(x,y) ) return 0 ;
int rc ;
if ( x.Year < y.Year ) rc = -1 ;
else if ( x.Year > y.Year ) rc = +1 ;
else // x.Year == y.Year
{
if ( x.Month < y.Month ) rc = -1 ;
else if ( x.Month > y.Month ) rc = +1 ;
else // x.Month == y.Month
{
if ( x.Day < y.Day ) rc = -1 ;
else if ( x.Day > y.Day ) rc = +1 ;
else // x.Day == y.Day
{
if ( x.Hour < y.Hour ) rc = -1 ;
else if ( x.Hour > y.Hour ) rc = +1 ;
else // x.Hour == y.Hour
{
if ( x.Minute < y.Minute ) rc = -1 ;
else if ( x.Minute > y.Minute ) rc = +1 ;
else // x.Minute = y.Minute
{
if ( x.Second < y.Second ) rc = -1 ;
else if ( x.Second > y.Second ) rc = -1 ;
else /* x.Second == y.Seconds */ rc = 0 ;
}
}
}
}
}
return rc ;
}
#endregion
}
#region IComparable<MyDate> Members
public int CompareTo( MyDate other )
{
return comparerInstance.Compare( this , other ) ;
}
#endregion
}