Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Standard view
Full view
of .
Look up keyword
Like this
0 of .
Results for:
No results containing your search query
P. 1
Using IComparer IComparable for Sorting in WPF

Using IComparer IComparable for Sorting in WPF

Ratings: (0)|Views: 0|Likes:
Published by Abhi

More info:

Published by: Abhi on Mar 12, 2012
Copyright:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as DOC, PDF, TXT or read online from Scribd
See more
See less





Using IComparer IComparable for Sorting in WPF
IComparer / IComparable are interfaces used to compare two objects. The implementingclass has to define the logic for sorting. It provides a way to customize the sort order of acollection.Implementation of IComparable in WPF. Here, we implement the CompareTo method andsort 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.
namespaceWpfApplication1{ /// ///Interaction logic for MainWindow.xaml /// public partial class MainWindow: Window {publicMainWindow(){InitializeComponent();List empList= newList(); empList.AddRange(newList(){newEmp { Name= "A", Age= 25, Address= "Test Str eetA"},newEmp { Name= "A", Age= 26, Address= "Test StreetA1"}, newEmp { Name= "B", Age= 25, Address= "Test StreetB"}, newEmp { Name= "C", Age= 26, Address= "Test StreetC"}, newEmp { Name= "Z", Age= 25, Address= "Test StreetZ"}, newEmp { Name= "D", Age= 26, Address= "Test StreetD"}}); empList.Sort();}} public class Emp  {public stringName {get;set; } public intAge {get;set; } public stringAddress {get;set; } }
public class Emp: IComparable {public stringName {get;set; } public intAge {get;set; } public stringAddress {get;set; }  #regionIComparable Members public intCompareTo(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 }}
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 stringName {get;set; } } // Sorting using IComparable based Age ascending orderList empList= newList(); empList.AddRange(newList(){newEmp { Name= "C25"}, newEmp { Name= "B35"},  newEmp { Name= "A15"}}); empList.Sort();C# compiler is not smart enough to figure out which instance of type "Emp" should comefirst etc.
IComparer interface:
But sometimes, we may need to sort a list of objects when the class does not implementIComparable<> interface and also we may need various kinds of sorting on that class like:
1.Sort Emp by Age in Ascending Order2.Sort Emp by Age in Descending Order3.Sort Emp by NameTo solve this problem, .NET provides a special interface called IComparer<> which has amethod Compare(), takes two object parameters X, Y and returns an int. Use of IComparer<> interface tells List how exactly you want to sort.
namespaceWpfApplication1{  ///  ///Interaction logic for MainWindow.xaml /// public partial class MainWindow: Window { publicMainWindow(){InitializeComponent();  // Sorting using IComparable based Age ascending orderList empList= newList(); empList.AddRange(newList(){newEmp { Name= "A25", Age= 25, Address= "Te st StreetA25"},newEmp { Name= "B35", Age= 35, Address= "Test StreetB3 5"},newEmp { Name= "C15", Age= 15, Address= "Test StreetC1 5"},newEmp { Name= "D45", Age= 45, Address= "Test StreetD4 5"},newEmp { Name= "E5", Age= 5, Address= "Test StreetE5"} });  // Default sort as defined by IComparableempList.Sort(); foreach(dynamicempinempList) { stringlistTemp=emp.Name+ "-" +emp.Age+ "-" +emp.Address; myListBox.Items.Add(listTemp); }  // Create IComparer instanceEmp_SortByAgeByDescendingOrder descSort= newEmp_SortByAgeByDescending Order();  // Specify the typeempList.Sort(descSort); foreach(dynamicempinempList) { stringlistTemp=emp.Name+ "-" +emp.Age+ "-" +emp.Address; myListBox1.Items.Add(listTemp); }}} public class Emp: IComparable { public stringName {get;set; }  public intAge {get;set; }  public stringAddress {get;set; }  public intCompareTo(Emp other) {  // Sort based on Ageif (this.Age>other.Age) {

You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->