如何比较多个对象值?

时间:2011-01-17 19:02:58

标签: c# icomparer

假设我有一个对象,其中包含以下值(另请注意我不希望使用日期时间对象,只需以下值,我希望在比较器中解决这个问题):

int year; 
int month; 
int day;
int sec;
int min;

如何比较我的Comparer中的所有多个值,以便按日期列出?

然后我想创建一个Comparer.cs类:

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

}

3 个答案:

答案 0 :(得分:7)

像这样:

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

答案 1 :(得分:1)

你需要继续进行比较,直到你有差异或者没有什么可比较的。

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;

答案 2 :(得分:0)

首先从最重要的元素开始比较,如下所示:

    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
    }

这只是无聊的管道工作,没有任何魔力。