Click here to Skip to main content
13,191,877 members (64,508 online)
Click here to Skip to main content
Add your own
alternative version


18 bookmarked
Posted 12 Feb 2005

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.


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

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.


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
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

You may also be interested in...


Comments and Discussions

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

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
Web01 | 2.8.171017.2 | Last Updated 12 Feb 2005
Article Copyright 2005 by Wesam Elsawirki
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid