SIGN UP MEMBER LOGIN:    
ARTICLE

Using IComparer IComparable for Sorting in WPF

Posted by Abhishek Bhat Articles | WPF with C# January 27, 2011
Implementation of IComparable in WPF. Here, we implement the CompareTo method and sort the Emp objects based on Age property.
Reader Level:


IComparer / IComparable are interfaces used to compare two objects. The implementing class has to define the logic for sorting. It provides a way to customize the sort order of a collection.

Implementation of IComparable in WPF. Here, we implement the CompareTo method and sort the Emp objects based on Age property.

Just create a sample WPF application and paste the code as seen below :

Emp class implements IComparable which defines the sort logic.

namespace WpfApplication1 

///
/// Interaction logic for MainWindow.xaml 
/// 
public partial class MainWindow : Window 

    public MainWindow() 
    { 
        InitializeComponent();
        List empList = new List(); 
        empList.AddRange(new List(){new Emp { Name = "A", Age = 25, Address = "Test StreetA" }, 
        new Emp { Name = "A", Age = 26, Address = "Test StreetA1" }, 
        new Emp { Name = "B", Age = 25, Address = "Test StreetB" }, 
        new Emp { Name = "C", Age = 26, Address = "Test StreetC" }, 
        new Emp { Name = "Z", Age = 25, Address = "Test StreetZ" }, 
        new Emp { Name = "D", Age = 26, Address = "Test StreetD" }}); 
        empList.Sort();
        } 
}
 
public class Emp 

    public string Name { getset; } 
    public int Age { getset; } 
    public string Address { getset; } 
}

 public class Emp : IComparable 

    public string Name { getset; } 
    public int Age { getset; } 
    public string Address { getset; }
 
    #region IComparable Members
    public int CompareTo(Emp other) 
    { 
        // Sort based on Age 
        if (this.Age > other.Age) 
        { 
            return 1
        } 
        else if (other.Age > this.Age) 
             return -1
        else 
             return 0;
        }
        #endregion 
    } 
}

Note:

List test = new List();
test.Add("B");
test.Add("C");
test.Add("A");
test.Sort();

This will work because by default List and Array implement IComparable.

But the example below won't work.

public class Emp 

    public string Name { getset; } 
}
// Sorting using IComparable based Age ascending order 
List empList = new List(); 
empList.AddRange(new List(){new Emp { Name = "C25"}, 
                            new Emp { Name = "B35"},
                            new Emp { Name = "A15"}});
empList.Sort();

C# compiler is not smart enough to figure out which instance of type "Emp" should come first etc.

IComparer interface:

But sometimes, we may need to sort a list of objects when the class does not implement IComparable<> interface and also we may need various kinds of sorting on that class like:

  1. Sort Emp by Age in Ascending Order
  2. Sort Emp by Age in Descending Order
  3. Sort Emp by Name
To solve this problem, .NET provides a special interface called IComparer<> which has a method Compare(), takes two object parameters X, Y and returns an int. Use of IComparer<> interface tells List how exactly you want to sort.

namespace WpfApplication1
{
    ///

     /// Interaction logic for MainWindow.xaml 
    /// 
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            // Sorting using IComparable based Age ascending order 
            List empList = new List();
            empList.AddRange(new List(){new Emp { Name = "A25", Age = 25, Address = "Test StreetA25" }, 
                                        new Emp { Name = "B35", Age = 35, Address = "Test StreetB35" }, 
                                        new Emp { Name = "C15", Age = 15, Address = "Test StreetC15" }, 
                                        new Emp { Name = "D45", Age = 45, Address = "Test StreetD45" }, 
                                        new Emp { Name = "E5", Age = 5, Address = "Test StreetE5" }});
            // Default sort as defined by IComparable 
            empList.Sort();
            foreach (dynamic emp in empList)
            {
                string listTemp = emp.Name + "-" + emp.Age + "-" + emp.Address;
                myListBox.Items.Add(listTemp);
            }
            // Create IComparer instance 
            Emp_SortByAgeByDescendingOrder descSort = new Emp_SortByAgeByDescendingOrder();
            // Specify the type 
            empList.Sort(descSort);
            foreach (dynamic emp in empList)
            {
                string listTemp = emp.Name + "-" + emp.Age + "-" + emp.Address;
                myListBox1.Items.Add(listTemp);
            }
        }
    }
    public class Emp : IComparable
    {
        public string Name { getset; }
        public int Age { getset; }
        public string Address { getset; }
        public int CompareTo(Emp other)
        {
            // Sort based on Age 
            if (this.Age > other.Age)
            {
                return 1;
            }
            else if (other.Age > this.Age)
                return -1;
            else
                return 0;
        }
    }
    class Emp_SortByAgeByDescendingOrder : IComparer
    {
        #region IComparer Members
        public int Compare(Emp x, Emp y)
        {
            if (x.Age < y.Age) return 1;
            else if (x.Age > y.Age) return -1;
            else return 0;
        }
        #endregion
    }
    class Emp_SortByName : IComparer
    {
        #region IComparer Members
        public int Compare(Emp x, Emp y)
        {
            return string.Compare(x.Name, y.Name);
        }

        #endregion

    }
}

Special case : If we have both IComparer and IComparable implemented Sort will take place based on IComparer logic first and then followed
by IComparable logic.

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        List empList = new List();
        empList.AddRange(new List(){new Emp { Name = "A", Age = 25, Address = "Test StreetA25" }, 
                                    new Emp { Name = "A", Age = 35, Address = "Test StreetB35" }, 
                                    new Emp { Name = "B", Age = 15, Address = "Test StreetC15" }, 
                                    new Emp { Name = "B", Age = 45, Address = "Test StreetD45" }, 
                                    new Emp { Name = "C", Age = 5, Address = "Test StreetE5" }});
        // Create IComparer instance 
        // IComparer sorts by Name asc 
        // IComparable sorts by Age desc 
        // Sort should be Name asc and Age desc 
        Emp_SortByName descSort = new Emp_SortByName();
        // Specify the type 
        empList.Sort(descSort);
        foreach (dynamic emp in empList)
        {
            string listTemp = emp.Name + "-" + emp.Age + "-" + emp.Address;
            myListBox1.Items.Add(listTemp);
        }
    }
}
 
public class Emp : IComparable
{
    public string Name { getset; }
    public int Age { getset; }
    public string Address { getset; }
    public int CompareTo(Emp other)
    {
        // Sort based on Age 
        if (this.Age > other.Age)
        {
            return 1;
        }
        else if (other.Age > this.Age)
            return -1;
        else
            return 0;
    }
}
 
class Emp_SortByName : IComparer
{
    #region IComparer Members
    public int Compare(Emp x, Emp y)
    {
        return string.Compare(x.Name, y.Name);
    }
    #endregion
}


Happy Reading...

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Become a Sponsor