C#
  Home arrow C# arrow Page 4 - Building C# Comparable Objects: IComparabl...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C#

Building C# Comparable Objects: IComparable versus IComparer
By: Ayad Boudiab
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 3
    2008-08-04

    Table of Contents:
  • Building C# Comparable Objects: IComparable versus IComparer
  • Sorting Objects
  • Quick Recap
  • Full Program

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Building C# Comparable Objects: IComparable versus IComparer - Full Program


    (Page 4 of 4 )


    Here is the full program:


    class Program

    {

    static   void Main( string [] args)

    {

    Employee[] employees = new Employee[5];

    employees[0] = new Employee( "Dan K." , 12456);

    employees[1] = new Employee( "Jim L." , 99584);

    employees[2] = new Employee( "Tony T." , 51472);

    employees[3] = new Employee( "Lee W." , 32788);

    employees[4] = new Employee( "Leda B." , 44110);


    Array .Sort(employees);

    PrintEmployees(employees);

    Console .WriteLine();

    Array .Sort(employees, Employee.SortByEmployeeName);

    PrintEmployees(employees);

    }


    static   void PrintEmployees(Employee[] employees)

    {

    foreach (Employee e in employees)

    Console .WriteLine(e);

    }

    }


    class Employee : IComparable

    {

    private   string name;

    public   string Name

    {

    get { return name; }

    set { name = value ; }

    }

    private   int id;

    public   int Id

    {

    get { return id; }

    set { id = value ; }

    }


    public Employee( string a_name, int an_id)

    {

    name = a_name;

    id = an_id;

    }


    public   override   string ToString()

    {

    return   string .Format( "Name: {0}ttId: {1}" ,

    Name, Id);

    }


    public   static   IComparer SortByEmployeeName

    {

    get { return ( IComparer ) new EmployeeComparer(); }

    }


    #region IComparable Members


    public   int CompareTo( object obj)

    {

    Employee temp = (Employee)obj;

    if ( this .Id > temp.Id)

    return 1;

    if ( this .Id < temp.Id)

    return -1;

    else

    return 0;

    }


    #endregion

    }


    class EmployeeComparer : IComparer

    {

    public EmployeeComparer() { }


    #region IComparer Members


    public   int Compare( object obj1, object obj2)

    {

    Employee e1 = (Employee)obj1;

    Employee e2 = (Employee)obj2;

    return   string .Compare(e1.Name, e2.Name);

    }


    #endregion

    }

    Conclusion

    The System.Array.Sort() methods sorts objects of different types. While sorting predefined types (string…) is already implemented in the language, sorting objects for classes that we create requires implementing interfaces to tell the runtime how to sort these objects. We can implement the Comparable interface and override its CompareTo() method and/or the Comparer interface with its Compare() method. The Sort() method is overloaded to support both implementations.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · Welcome to my latest C# article. I am looking forward to your feedback.
       · Excellent work. New things are introduced at the right time in this article, that...
       · Excellent work. New things are introduced at the right time in this article, that...
     

    C# ARTICLES

    - Working with Dates and Times in C#
    - Generics, Dictionaries, and More
    - More About Generics
    - Working with C# Collections
    - Generics
    - C# and XML
    - Pointers and Arrays in C#
    - C# 3.0 Extension Methods
    - Overloading Operators in C#
    - Iterators and Nullable Types
    - Patterns and Iterators in C#
    - C# Exceptions
    - Methods in C#
    - Delegates and Events in C#
    - Advanced C#

     
    Best Practices for Windows Vista Migration Presentation
    Dell and Microsoft recently held a series of face-to-face seminars entitled, &qu....

     
    Creating a Culture for Code Reuse
    If you oversee development teams you know that like it or not proprietary and ex....

     
    Keys to Web Application Acceleration: Advances in Delivery Systems
    Accelerate Web apps by up to 5x. Ensure significantly faster access to the Web a....

     
    Optimizing Application Monitoring
    Tired of finding out from your customers that you're offline? This white paper e....

     
    Solaris to Solaris Migration -- Migrating applications from Sun SPARC to Dell PowerEdge R900
    This comprehensive Migration Guide reviews the approach that Principled Technolo....

     




    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT