Click here to Skip to main content
15,893,904 members
Articles / Programming Languages / C#
Article

Sorting objects on their properties that do not have their own CompatreTo() implementation.

Rate me:
Please Sign up or sign in to vote.
1.29/5 (6 votes)
15 Mar 20061 min read 17.4K   124   6   4
Shows how to Sort objects on their properties that do not have their own CompatreTo() implementation with an example of Colored Balls in Pipe using GDI+.

Sample Image - Sort.jpg

Introduction

Let us take an example of Student objects, with properties Id (int), Name (String), Height (double) etc. If we need to sort a collection (say an Array or Arraylist) of student objects on their Ids, or Names or Heights all we need to do is, to implement the IComparable interface and use the CompareTo() method of the respective data type in the implementation for the CompareTo() method in the Student Class.

For example if i need to sort on the Id (an int), the following would be the implementation of the compareTo() function.

//comparing the int<BR>public int CompareTo(object obj) <BR>{<BR> if(obj is Student) <BR> {<BR>  Student s = (Student) obj;<BR>  return _nId.CompareTo(s._nId);<BR> }<BR> throw new ArgumentException("object is not a Student"); <BR>}<BR>

But what if i add a new property called EyeColor, which is of Type System.Drawing.Color that does not have its own CompareTo() Implementation?

We need to supply an implementation that tells the Array or the ArrayList how the two instances are decided as > or = or < when compared to each other.

I could sort out this problem by using the GetHashcode() of Color instance, which returns an integer which inturn has its own CompareTo() method to be readily used.

I wrote a sample application using this logic to sort colored balls in a pipe, in groups of the simillar color.

There are 2 classes one for pipe and the other for the Ball. In the ball implements the IComparable interface and its CompareTo() is as follows...

<BR>//comparing the color<BR>public int CompareTo(object obj) <BR>{<BR> if(obj is BallGDI) <BR> { <BR> BallGDI b = (BallGDI) obj;<BR> return _Colour.GetHashCode().CompareTo(b._Colour.GetHashCode());<BR> }<BR> throw new ArgumentException("object is not a Ball"); <BR>}<BR><BR>

 

 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Team Leader
India India
Techincal Architect

16 years of overall work experience.

Proven Expertise in Agile process implementation.

Managed complex projects through their lifecycle.

Methodologies used include Agile, SDLC, Waterfall models.

Comments and Discussions

 
GeneralSolution is fundamentally wrong Pin
wout de zeeuw15-Mar-06 7:36
wout de zeeuw15-Mar-06 7:36 
GeneralRe: Solution is fundamentally wrong Pin
B.Sai Suryanarayana24-Nov-11 19:40
B.Sai Suryanarayana24-Nov-11 19:40 
GeneralRe: Solution is fundamentally wrong Pin
wout de zeeuw24-Nov-11 22:00
wout de zeeuw24-Nov-11 22:00 
GeneralRe: Solution is fundamentally wrong Pin
B.Sai Suryanarayana22-Dec-11 2:07
B.Sai Suryanarayana22-Dec-11 2:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.