Click here to Skip to main content
Licence 
First Posted 12 Feb 2005
Views 51,712
Bookmarked 17 times

Sorting custom type elements stored in an ArrayList

By | 12 Feb 2005 | Article
Sorting custom type elements stored in an ArrayList.

Introduction

Many times we find ourselves in a situation where we need to sort custom type elements stored in an ArrayList according to some property of the element. In fact, I could not find anybody to help me with this problem, and that's why I decided to deal with this problem - and shared the solution I found with others here on CodeProject.

Solving the problem

Suppose we have some structure with only an integer and a string property which will be used in the sort operation later. For the sake of simplicity let's suppose we have the following structure:

public struct MyStrcuture
{
   public Int32 iID;
   public String sName;
}

In order to be able to sort custom type elements contained in an ArrayList, we have to define the CompareTo method of the IComparable interface for the element, and then use the ArrayList's .Sort() method. This -of course- means that our structure should be inherited from that interface .. and so we'll do!

public struct MyStrcuture : IComparable
{
   public Int32 iID;
   public String sName;

   public Int32 CompareTo(Object obj) 
   {
      MyStrcuture tmpObj = (MyStrcuture) obj;
      return (this.sName.CompareTo(tmpObj.sName));
   }
}

As you see, it is in the structure where we define the .CompareTo() method and you as a developer could write any code that suits your needs.

After this we need to test all this, let's suppose we fill an ArrayList with elements of MyStructure type:

ArrayList arlElements = new ArrayList();
for (Int32 iCounter = 1; iCounter <= 20; iCounter++)
{
   // Preparing element
   MyStructure myData = new MyStructure();
   myData.iID = iCounter;
   myData.sName = iCounter.ToString();

   //Adding element
   arlElements.Add(myData);
}

In order to sort, we only need to call the .Sort() method :-): arlElements.Sort();.

Hope this was useful. In the demo program you can find a workable version of the program with slight changes that do not alter its main concept.

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

Wesam Elsawirki

Software Developer (Senior)

Hungary Hungary

Member

Professional Software Developer, currently working for HVB bank.
Programming experience includes: C#, ASP.NET, Delphi, PHP, VB.
Technologies: MS SQL Server 2005, Apache, IIS.
 
Certifications: MCAD, MCSD, MCTS (MS SQL Server 2005), MCITP

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralQuestion Pinmembermp12341:54 9 May '07  
GeneralRe: Question PinmemberWesam E.23:56 14 May '07  
GeneralRe: Question Pinmembermp123423:40 15 May '07  
Questionquestion Pinmemberbissoi7:02 27 Mar '07  
AnswerRe: question PinmemberWesam E.4:21 28 Mar '07  
QuestionHow Sort Based on Mulitple Object Pinmemberraj_3272:15 20 Mar '07  
AnswerRe: How Sort Based on Mulitple Object PinmemberWesam E.4:33 28 Mar '07  
GeneralThanks ..but please help me. [modified] Pinmemberteddynatt6:42 20 Oct '06  
GeneralRe: Thanks ..but please help me. PinmemberWesam E.0:53 21 Oct '06  
GeneralRe: Thanks ..but please help me. Pinmemberteddynatt4:27 21 Oct '06  
GeneralThanks [modified] PinmemberXenion18:10 30 Jun '06  
GeneralRe: Thanks PinmemberWesam E.23:50 30 Jun '06  
GeneralThanks PinmemberStephen083811:49 26 Oct '05  
GeneralRe: Thanks PinsussAnonymous20:51 26 Oct '05  
GeneralTanks! PinmemberColores213:23 19 Sep '05  
GeneralRe: Tanks! PinmemberWesam E.12:21 20 Sep '05  
Generalneed help Pinmembergpascanu22:36 7 Sep '05  
GeneralGave it a good score. PinmemberAshaman7:32 14 Feb '05  
GeneralRe: Gave it a good score. PinmemberWesam E.12:11 14 Feb '05  
GeneralRe: Gave it a good score. Pinmemberjeweladdict13:06 13 Sep '06  

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.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 12 Feb 2005
Article Copyright 2005 by Wesam Elsawirki
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid