Click here to Skip to main content
15,886,199 members
Articles / Programming Languages / C#
Article

Sorting custom type elements stored in an ArrayList

Rate me:
Please Sign up or sign in to vote.
3.56/5 (36 votes)
12 Feb 20051 min read 72.4K   696   18   22
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:

C#
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!

C#
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:

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


Written By
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 1004329013-Mar-15 23:37
Member 1004329013-Mar-15 23:37 
GeneralMy vote of 5 Pin
codejuggler926-Dec-12 14:32
codejuggler926-Dec-12 14:32 
GeneralQuestion Pin
mp12349-May-07 1:54
mp12349-May-07 1:54 
GeneralRe: Question Pin
Wesam Elsawirki14-May-07 23:56
Wesam Elsawirki14-May-07 23:56 
GeneralRe: Question Pin
mp123415-May-07 23:40
mp123415-May-07 23:40 
Smile | :) thanx
Questionquestion Pin
bissoi27-Mar-07 7:02
bissoi27-Mar-07 7:02 
AnswerRe: question Pin
Wesam Elsawirki28-Mar-07 4:21
Wesam Elsawirki28-Mar-07 4:21 
QuestionHow Sort Based on Mulitple Object Pin
raj_32720-Mar-07 2:15
raj_32720-Mar-07 2:15 
AnswerRe: How Sort Based on Mulitple Object Pin
Wesam Elsawirki28-Mar-07 4:33
Wesam Elsawirki28-Mar-07 4:33 
GeneralThanks ..but please help me. [modified] Pin
teddynatt20-Oct-06 6:42
teddynatt20-Oct-06 6:42 
GeneralRe: Thanks ..but please help me. Pin
Wesam Elsawirki21-Oct-06 0:53
Wesam Elsawirki21-Oct-06 0:53 
GeneralRe: Thanks ..but please help me. Pin
teddynatt21-Oct-06 4:27
teddynatt21-Oct-06 4:27 
GeneralThanks [modified] Pin
Xenion30-Jun-06 18:10
Xenion30-Jun-06 18:10 
GeneralRe: Thanks Pin
Wesam Elsawirki30-Jun-06 23:50
Wesam Elsawirki30-Jun-06 23:50 
GeneralThanks Pin
Stephen083826-Oct-05 11:49
Stephen083826-Oct-05 11:49 
GeneralRe: Thanks Pin
Anonymous26-Oct-05 20:51
Anonymous26-Oct-05 20:51 
GeneralTanks! Pin
Colores219-Sep-05 13:23
Colores219-Sep-05 13:23 
GeneralRe: Tanks! Pin
Wesam Elsawirki20-Sep-05 12:21
Wesam Elsawirki20-Sep-05 12:21 
Generalneed help Pin
gpascanu7-Sep-05 22:36
gpascanu7-Sep-05 22:36 
GeneralGave it a good score. Pin
Ashaman14-Feb-05 7:32
Ashaman14-Feb-05 7:32 
GeneralRe: Gave it a good score. Pin
Wesam Elsawirki14-Feb-05 12:11
Wesam Elsawirki14-Feb-05 12:11 
GeneralRe: Gave it a good score. Pin
jeweladdict13-Sep-06 13:06
jeweladdict13-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.