Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare multiple object values against each other?

Tags:

c#

icomparer

Assume i have a object with the following values in it (also please note i do not wish to use a datetime object for this, just the following values below and i wish to solve this in the comparer itself):

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

How can i compare all those multiple values in my Comparer against each other so they get listed according date?

Then i wish to make a Comparer.cs class:

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

}

like image 398
Neaer Avatar asked Oct 15 '25 15:10

Neaer


1 Answers

Like this:

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
like image 50
SLaks Avatar answered Oct 17 '25 06:10

SLaks



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!