Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Generic Sort Class

Generic Sort Class

Scheduled Pinned Locked Moved ASP.NET
algorithmsquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tiger456
    wrote on last edited by
    #1

    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

    C 1 Reply Last reply
    0
    • T Tiger456

      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

      C Offline
      C Offline
      chintan1979
      wrote on last edited by
      #2

      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

      T 1 Reply Last reply
      0
      • C chintan1979

        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

        T Offline
        T Offline
        Tiger456
        wrote on last edited by
        #3

        Thanks for this great code :):):):):)

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups