Click here to Skip to main content
Click here to Skip to main content

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

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

About the Author

B.Sai Suryanarayana
Team Leader
India India
A unique combination of management and innovative thinking coupled with good architecting and designing skills in the Information Technology industry
 
Around 9 years of overall work experience (5 years in Development, 3+ years in Project Management)
2 years experience in product development.
 
Managed complex projects through their lifecycle. Methodologies used include SDLC, Waterfall models.
 
Proven Expertise in Agile process implementation.
 
Estimation Techniques:
PERT, UCP and FPA
 
EXPERTISE: Architecture/Design;
Concept development; Systems Designing

RECENT ACHIEVEMENTS: As a Project Manager & Architect to Design, Develop and Deliver a Data-security tool for securing sensitive information in database.

Comments and Discussions

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

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

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

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