Click here to Skip to main content
12,943,299 members (66,490 online)
Click here to Skip to main content
Add your own
alternative version


6 bookmarked
Posted 15 Mar 2006

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

, 15 Mar 2006
Rate this:
Please Sign up or sign in to vote.
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


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>




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


About the Author

B.Sai Suryanarayana
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
memberwout de zeeuw15-Mar-06 7:36 
GeneralRe: Solution is fundamentally wrong Pin
B.Sai Suryanarayana24-Nov-11 19:40
memberB.Sai Suryanarayana24-Nov-11 19:40 
GeneralRe: Solution is fundamentally wrong Pin
wout de zeeuw24-Nov-11 22:00
memberwout de zeeuw24-Nov-11 22:00 
GeneralRe: Solution is fundamentally wrong Pin
B.Sai Suryanarayana22-Dec-11 2:07
memberB.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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170518.1 | Last Updated 15 Mar 2006
Article Copyright 2006 by B.Sai Suryanarayana
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid