Home

Built-In Interfaces: IComparable

     

Introduction

Comparing two objects consists of finding out which one comes before the other. The comparison is simple if you are dealing with values of primitive types. For example, it is easy to know that 2 is lower than 5, but it is not obvious to compare two objects created from a composite type, such as two students, two cars, or two food items. Consider the following Student class:

 
using System;

public enum Genders { Male, Female, Unknown }

public class Student : IComparable
{
    private long sn;
    private string fn;
    private string ln;
    private float howOld;
    private Genders gd;

    public Student(long number = 0, string first = "John",
    	 	   string last = "Doe", float ag = 1,
    	 	   Genders gdr = Genders.Unknown)
    {
        sn = number;
        fn = first;
        ln = last;
        howOld = ag;
        gd = gdr;
    }

    public long StudentNumber
    {
        get { return sn; }
        set { sn = value; }
    }

    public string FirstName
    {
        get { return fn; }
        set { fn = value; }
    }

    public string LastName
    {
        get { return ln; }
        set { ln = value; }
    }

    public float Age
    {
        get { return howOld; }
        set { howOld = value; }
    }

    public Genders Gender
    {
        get { return gd; }
        set { gd = value; }
    }

    public int CompareTo(object obj)
    {
        return 0;
    }
}

public class Exercise
{
     public static int Main()
     {
         Student std = new Student(294759, "Patricia",
                                   "Katts", 14.50F, Genders.Female);

         Console.WriteLine("Student Information");
         Console.WriteLine("-----------------------");
         Console.WriteLine("Student #:  {0}", std.StudentNumber);
         Console.WriteLine("First Name: {0}", std.FirstName);
         Console.WriteLine("Last Name:  {0}", std.LastName);
         Console.WriteLine("Age:        {0}", std.Age);
         Console.WriteLine("Gender:     {0}", std.Gender);
         Console.WriteLine("=======================\n");

         return 0;
     }
}

This would produce:

Student Information
-----------------------
Student #:  294759
First Name: Patricia
Last Name:  Katts
Age:        14.5
Gender:     Female
=======================

Press any key to continue . . .

To assist you with comparing two objects, the .NET Framework provides various comparable interfaces. One of these interfaces is named IComparable. The IComparable interface is a member of the System namespace. Obviously you must define what would be compared and how the comparison would be carried. For example, to compare two Student objects of the above class, you can ask the compiler to base the comparison on the student number. Here is an example:

using System;

public enum Genders { Male, Female, Unknown }

public class Student : IComparable
{
    . . . No Change

    public int CompareTo(object obj)
    {
        Student std = (Student)obj;

        return std.sn.CompareTo(this.sn);
    }
}

public class Exercise
{
    public static int Main()
    {
        Student std1 = new Student(294759, "Patricia",
                                  "Katts", 14.50F, Genders.Female);
        Student std2 = new Student(294706, "Raymond",
                                  "Kouma", 18.50F, Genders.Male);
        Student std3 = new Student(747747, "Patricia",
                                  "Childs", 12.00F, Genders.Female);

        Console.WriteLine("Comparison by Student Number");
        Console.WriteLine("First => Second: {0}", std1.CompareTo(std2));
        Console.WriteLine("First => Third:  {0}", std1.CompareTo(std3));

        return 0;
    }
}

This would produce:

Comparison by Student Number
First => Second: -1
First => Third:  1
Press any key to continue . . .

In the same way, you can choose any other member of the class to base the comparison. An example would consist of comparing the last names of students. Here is an example:

using System;

public enum Genders { Male, Female, Unknown }

public class Student : IComparable
{
    private long sn;
    private string fn;
    private string ln;
    private float howOld;
    private Genders gd;

    public Student(long number = 0, string first = "John",
    	 	   string last = "Doe", float ag = 1,
    	 	   Genders gdr = Genders.Unknown)
    {
        sn = number;
        fn = first;
        ln = last;
        howOld = ag;
        gd = gdr;
    }

    public long StudentNumber
    {
        get { return sn; }
        set { sn = value; }
    }

    public string FirstName
    {
        get { return fn; }
        set { fn = value; }
    }

    public string LastName
    {
        get { return ln; }
        set { ln = value; }
    }

    public float Age
    {
        get { return howOld; }
        set { howOld = value; }
    }

    public Genders Gender
    {
        get { return gd; }
        set { gd = value; }
    }

    public int CompareTo(object obj)
    {
        Student std = (Student)obj;

        return std.ln.CompareTo(this.ln);
    }
}

public class Exercise
{
    public static int Main()
    {
        Student std1 = new Student(294759, "Patricia",
                                  "Katts", 14.50F, Genders.Female);
        Student std2 = new Student(294706, "Raymond",
                                  "Kouma", 18.50F, Genders.Male);
        Student std3 = new Student(747747, "Patricia",
                                  "Childs", 12.00F, Genders.Female);
        Student std4 = new Student(500402, "Leslie",
                                  "Katts", 12.00F, Genders.Unknown);

        Console.WriteLine("Comparison by Student Number");
        Console.WriteLine("First => Second: {0}", std1.CompareTo(std2));
        Console.WriteLine("First => Third:  {0}", std1.CompareTo(std3));
        Console.WriteLine("First => Fourth: {0}", std1.CompareTo(std4));

        return 0;
    }
}

This would produce:

Comparison by Student Number
First => Second: 1
First => Third:  -1
First => Fourth: 0
Press any key to continue . . .

Most of the .NET Framework's classes that need to perform comparison already implement the IComparable interface or one of its equivalents.

 
 

Home Copyright © 2010-2011 FunctionX