Click here to Skip to main content
11,436,179 members (63,379 online)
Click here to Skip to main content

Sorting custom type elements stored in an ArrayList

, 12 Feb 2005
Rate this:
Please Sign up or sign in to vote.
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 Smile | :) : 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

Share

About the Author

Wesam Elsawirki
Software Developer (Senior)
Hungary Hungary
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

Comments and Discussions

 
QuestionThanks Pin
Member 1004329014-Mar-15 0:37
memberMember 1004329014-Mar-15 0:37 
GeneralMy vote of 5 Pin
codejuggler926-Dec-12 15:32
membercodejuggler926-Dec-12 15:32 
GeneralQuestion Pin
mp12349-May-07 2:54
membermp12349-May-07 2:54 
GeneralRe: Question Pin
Wesam E.15-May-07 0:56
memberWesam E.15-May-07 0:56 
GeneralRe: Question Pin
mp123416-May-07 0:40
membermp123416-May-07 0:40 
Questionquestion Pin
bissoi27-Mar-07 8:02
memberbissoi27-Mar-07 8:02 
AnswerRe: question Pin
Wesam E.28-Mar-07 5:21
memberWesam E.28-Mar-07 5:21 
QuestionHow Sort Based on Mulitple Object Pin
raj_32720-Mar-07 3:15
memberraj_32720-Mar-07 3:15 
AnswerRe: How Sort Based on Mulitple Object Pin
Wesam E.28-Mar-07 5:33
memberWesam E.28-Mar-07 5:33 
GeneralThanks ..but please help me. [modified] Pin
teddynatt20-Oct-06 7:42
memberteddynatt20-Oct-06 7:42 
GeneralRe: Thanks ..but please help me. Pin
Wesam E.21-Oct-06 1:53
memberWesam E.21-Oct-06 1:53 
GeneralRe: Thanks ..but please help me. Pin
teddynatt21-Oct-06 5:27
memberteddynatt21-Oct-06 5:27 
GeneralThanks [modified] Pin
Xenion30-Jun-06 19:10
memberXenion30-Jun-06 19:10 
GeneralRe: Thanks Pin
Wesam E.1-Jul-06 0:50
memberWesam E.1-Jul-06 0:50 
GeneralThanks Pin
Stephen083826-Oct-05 12:49
memberStephen083826-Oct-05 12:49 
GeneralRe: Thanks Pin
Anonymous26-Oct-05 21:51
sussAnonymous26-Oct-05 21:51 
GeneralTanks! Pin
Colores219-Sep-05 14:23
memberColores219-Sep-05 14:23 
GeneralRe: Tanks! Pin
Wesam E.20-Sep-05 13:21
memberWesam E.20-Sep-05 13:21 
Generalneed help Pin
gpascanu7-Sep-05 23:36
membergpascanu7-Sep-05 23:36 
GeneralGave it a good score. Pin
Ashaman14-Feb-05 8:32
memberAshaman14-Feb-05 8:32 
GeneralRe: Gave it a good score. Pin
Wesam E.14-Feb-05 13:11
memberWesam E.14-Feb-05 13:11 
GeneralRe: Gave it a good score. Pin
jeweladdict13-Sep-06 14:06
memberjeweladdict13-Sep-06 14: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.

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