11

I've made myself a rss reader that keeps me up to date and informs me on new shows, or atleast thats the thought behind.

I've made a struct "SeasonEpisode" that hold two ints (season+episode) and a override ToString function.

I store the latest watched locally and i then read whats the newest is from the rss. But how could I compare SeasonEpisodes? right now I take each of the ints and compare them

if( se1.Season >= se2.Season )
    if( se1.Episode > se2.Episode || se1.Season > se2.Season )
        // new episode!

What i really want is

if( se1 > se2 )
    // new episode

Could i get any help please?

Jason94
  • 13,320
  • 37
  • 106
  • 184

3 Answers3

37

There are two ways:

  1. Implement IComparable<T> and use CompareTo
  2. Overload the greater and less than operators

I suggest, you use both ways:

public class SeasonEpisode : IComparable<SeasonEpisode>
{
    public int CompareTo(SeasonEpisode other)
    {
        if(other == null)
            return 1;
        if(Season == other.Season)
        {
            if(Episode == other.Episode)
                return 0;
            else if(Episode < other.Episode)
                return -1;
            else
                return 1;
        }
        else if(Season < other.Season) 
            return -1;
        else
            return 1;
    }

    public static bool operator <(SeasonEpisode e1, SeasonEpisode e2) 
    {
        return e1.CompareTo(e2) < 0;
    }

    public static bool operator >(SeasonEpisode e1, SeasonEpisode e2) 
    {
        return e1.CompareTo(e2) > 0;
    }
}
Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
3

As i tripped over a NullReferenceException, here's an improvement (well this may be subjective ;-)) to Daniel Hilgarth's answer.

The only change is that it handles nulls in case the first argument to the > or < operator is null:

public class SeasonEpisode : IComparable<SeasonEpisode>
{
    private static int Compare(SeasonEpisode e1, SeasonEpisode e2)
    {
        if (e1 == null && e2 == null)
            return 0;
        else if (e1 == null)
            return -1;
        else if (e2 == null)
            return 1;

        if(e1.Season == e2.Season)
        {
            if(e1.Episode == e2.Episode)
                return 0;
            else if(e1.Episode < e2.Episode)
                return -1;
            else
                return 1;
        }
        else if(e1.Season < e2.Season) 
            return -1;
        else
            return 1;
    }    

    public int CompareTo(SeasonEpisode other)
    {
        return Compare(this, other);
    }

    public static bool operator <(SeasonEpisode e1, SeasonEpisode e2) 
    {
        return Compare(e1, e2) < 0;
    }

    public static bool operator >(SeasonEpisode e1, SeasonEpisode e2) 
    {
        return Compare(e1, e2) > 0;
    }
}
Community
  • 1
  • 1
BatteryBackupUnit
  • 12,934
  • 1
  • 42
  • 68
1

You can implement the IComparer<T> interface

Defines a method that a type implements to compare two objects.

You can implement IComparable if you want a class to be comparable to another instance of that class. Which is probably what you want, in this case.

Implement IComparer if you need a class that compares two objects.

KMån
  • 9,896
  • 2
  • 31
  • 41
  • 1
    (Worded for the old version of your answer, the edit of your answer didn't change the notion of my comment, though. Too lazy to re-word...) That's not really the correct interface to use here. You would either need a comparer class that implements this interface or implement it in `SeasonEpisode` and call it like this: `se1.Compare(se1, se2)`. A bit ackward. `IComparable` is what you want to use here. See also: http://stackoverflow.com/questions/538096/when-to-use-icomparablet-vs-icomparert – Daniel Hilgarth Apr 15 '11 at 06:21