Use Array.Sort to sort object array : Array Sort : Data Structure C# Examples


C# Examples » Data Structure » Array Sort »

 

Use Array.Sort to sort object array









    
using  System;
using  System.Collections.Generic;
using  System.Text;

class  MyClass  :  IComparable                                        
{
      public  int  TheValue;

      public  int  CompareTo(object  obj)                        
      {
            MyClass  mc  =  (MyClass)obj;
            
            if  (this.TheValue  <  mc.TheValue)  
                  return  -1;
            
            if  (this.TheValue  >  mc.TheValue)  
                  return  1;
            
            return  0;
      }
}

class  MainClass
{
      static  void  Main()
      {
            MyClass[]  objectArray  =  new  MyClass[5];                  
            for  (int  i  =  0;  i  <  5;  i++)                            
            {
                  objectArray[i]  =  new  MyClass();
                  objectArray[i].TheValue  =  100  -  i;
            }

            foreach  (MyClass  i  in  objectArray)
                  Console.Write("{0}  ",  i.TheValue);

            Array.Sort(objectArray);                                                

            foreach  (MyClass  i  in  objectArray)
                  Console.Write("{0}  ",  i.TheValue);
      }
}
    
   
  
   



Output

100 99 98 97 96 96 97 98 99 100


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Data Structure
» Array Sort