Wednesday 15 February 2017

IComparable

IComparable<T> defines a generic function that facilitates the sorting of instances of the type that implements it. For example if we had our person class and implemented the "CompareTo" function;

class Person : IComparable<Person>
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime BirthDate { get; set; }
    public int Age
    {
        get
        {
            var today = DateTime.Today;
            var age = today.Year - BirthDate.Year;
            return BirthDate > today.AddYears(-age) ? --age : age;
        }
    }

    public override string ToString()
    {
        return $"{Age}) {FirstName} {LastName}";
    }

    public int CompareTo(Person other)
    {
        return Age - other.Age;
    }

}

we see that the "CompareTo" function has three types of results,

  • a negative int: which means that the other person is has a higher age and will be to the right of the current person
  • a zero: which mean that the two people are of equal age and will be next to each other
  • a positive int: meaning that the other person is younger and will be to the left of the current Person.

Now let's demonstrate the IComparable interface in action

using System;
using System.Collections.Generic;

namespace pc.IComparable
{
    class Person : IComparable<Person>
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public DateTime BirthDate { get; set; }
        public int Age
        {
            get
            {
                var today = DateTime.Today;
                var age = today.Year - BirthDate.Year;
                return BirthDate > today.AddYears(-age) ? --age : age;
            }
        }

        public override string ToString()
        {
            return $"{Age}) {FirstName} {LastName}";
        }

        public int CompareTo(Person other)
        {
            return Age - other.Age;
        }
    }

class Program
{
    static void Main(string[] args)
    {
        var p1 = new Person { FirstName = "Tomek", LastName = "Chooch",
            BirthDate = new DateTime(1988, 8, 28) };
        var p2 = new Person { FirstName = "Pawel", LastName = "Chooch",
            BirthDate = new DateTime(1984, 6, 28) };
        var p3 = new Person { FirstName = "Marin", LastName = "Smartz",
            BirthDate = new DateTime(1983, 11, 3) };
        var p4 = new Person { FirstName = "Magda", LastName = "Chooch",
            BirthDate = new DateTime(1984, 1, 31) };
        var p5 = new Person { FirstName = "Jakeb", LastName = "Tywonk",
            BirthDate = new DateTime(1988, 5, 4) };
        var p6 = new Person { FirstName = "Ivanp", LastName = "endasd",
            BirthDate = new DateTime(1986, 3, 3) };
        var p7 = new Person { FirstName = "Jakes", LastName = "gadicd",
            BirthDate = new DateTime(1986, 3, 2) };
        var p8 = new Person { FirstName = "Bober", LastName = "asdasd",
            BirthDate = new DateTime(1986, 3, 1) };

        var ppl = new List<Person>(new Person[] { p1, p2, p3, p4, p5, p6, p7, p8 });
        ppl.Sort();

        foreach (var p in ppl)
            Console.WriteLine(p.ToString());
    }
}

As you can see we create 8 instances of our person class put them into an array, sort it and then output them in their sorted by age.