Generic Sort Class
-
Hai I am sorting an arraylist of objects using IComparer My code is
public class Employee { private string \_id; private string \_name; private string \_city; public Employee(string id , string name) { \_id = id; \_name = name; } public string Id { get { return \_id; } set { \_id = value; } } public string Name { get { return \_name; } set { \_name = value; } } public string City { get { return \_city; } set { \_city = value; } } public override string ToString() { return string.Format("ID : {0} , Name : {1} ",Id,Name); } } class SorterByID : IComparer { public int Compare(Object x , object y ) { Employee obj1 = (Employee) x; Employee obj2 = (Employee) y; return obj1.Id.CompareTo(obj2.Id); } } class SorterByName : IComparer { public int Compare(Object x , object y ) { Employee obj1 = (Employee) x; Employee obj2 = (Employee) y; return obj1.Name.CompareTo(obj2.Name); } }
to sort by ID
arrlist.Sort(new SorterByID());If i need to sort the objects with city then i need to implement one more class with iComparer inface . So my question is , i there any way to create it in a Generic way . Means with any property i can sort the objects Thanks and Regards
-
Hai I am sorting an arraylist of objects using IComparer My code is
public class Employee { private string \_id; private string \_name; private string \_city; public Employee(string id , string name) { \_id = id; \_name = name; } public string Id { get { return \_id; } set { \_id = value; } } public string Name { get { return \_name; } set { \_name = value; } } public string City { get { return \_city; } set { \_city = value; } } public override string ToString() { return string.Format("ID : {0} , Name : {1} ",Id,Name); } } class SorterByID : IComparer { public int Compare(Object x , object y ) { Employee obj1 = (Employee) x; Employee obj2 = (Employee) y; return obj1.Id.CompareTo(obj2.Id); } } class SorterByName : IComparer { public int Compare(Object x , object y ) { Employee obj1 = (Employee) x; Employee obj2 = (Employee) y; return obj1.Name.CompareTo(obj2.Name); } }
to sort by ID
arrlist.Sort(new SorterByID());If i need to sort the objects with city then i need to implement one more class with iComparer inface . So my question is , i there any way to create it in a Generic way . Means with any property i can sort the objects Thanks and Regards
Hi, Use this class. In oyur code You can write. arr.Sort(new ObjectComparer("Name", True)); [Serializable] public class ObjectComparer : IComparer { #region methods /// /// Compares two objects and returns a value indicating whether one is less than, equal to or greater than the other. /// /// First object to compare. /// Second object to compare. /// public int Compare(object x, object y) { //Get types of the objects Type typex = x.GetType(); Type typey = y.GetType(); //Get each property by name PropertyInfo pix = typex.GetProperty(Fields); PropertyInfo piy = typey.GetProperty(Fields); //Get the value of the property for each object IComparable pvalx = (IComparable)pix.GetValue(x, null); object pvaly = piy.GetValue(y, null); //Compare values, using IComparable interface of the property's type int iResult = pvalx.CompareTo(pvaly); if (iResult != 0) { //Return if not equal if (Descending) { //Invert order return -iResult; } else { return iResult; } } //Objects have the same sort order return 0; } #endregion #region constructors /// /// Create a comparer for objects of arbitrary types having using the specified properties and sort order /// /// Properties to sort objects by /// Properties to sort in descending order public ObjectComparer(string fields, bool descending) { Fields = fields; Descending = descending; } #endregion #region protected fields /// /// Properties to sort objects by /// protected string Fields; /// /// Properties to sort in descending order /// protected bool Descending; #endregion } Chintan
-
Hi, Use this class. In oyur code You can write. arr.Sort(new ObjectComparer("Name", True)); [Serializable] public class ObjectComparer : IComparer { #region methods /// /// Compares two objects and returns a value indicating whether one is less than, equal to or greater than the other. /// /// First object to compare. /// Second object to compare. /// public int Compare(object x, object y) { //Get types of the objects Type typex = x.GetType(); Type typey = y.GetType(); //Get each property by name PropertyInfo pix = typex.GetProperty(Fields); PropertyInfo piy = typey.GetProperty(Fields); //Get the value of the property for each object IComparable pvalx = (IComparable)pix.GetValue(x, null); object pvaly = piy.GetValue(y, null); //Compare values, using IComparable interface of the property's type int iResult = pvalx.CompareTo(pvaly); if (iResult != 0) { //Return if not equal if (Descending) { //Invert order return -iResult; } else { return iResult; } } //Objects have the same sort order return 0; } #endregion #region constructors /// /// Create a comparer for objects of arbitrary types having using the specified properties and sort order /// /// Properties to sort objects by /// Properties to sort in descending order public ObjectComparer(string fields, bool descending) { Fields = fields; Descending = descending; } #endregion #region protected fields /// /// Properties to sort objects by /// protected string Fields; /// /// Properties to sort in descending order /// protected bool Descending; #endregion } Chintan