C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » .NET Framework »

Sorting a ArrayList in C#


Posted Date: 26 Dec 2005    Resource Type: Articles    Category: .NET Framework
Author: vivekthangaswamyMember Level: Gold    
Rating: 1 out of 5Points: 14



Introduction


This article explains you how to customize sorting in ArrayList in C#. In this example I have put together a simple class called Category. A Category object one contains two member variables, one is Category Type and Category Type code.

Code snippet:

using System;

namespace ArrayListSORTING
{
public class Category
{
public string Type;
public int TypeCode;

public Category()
{
}

public Category(string type, int tcode)
{
this.Type = type;
this.TypeCode = tcode;
}
}
}


Business logic For Sorting



Our comparer is called CategoryComparer, and we also have an enum called SortDirection which allows us to define whether we want to sort in an Ascending or in Descending order.


using System;
using System.Collections;

namespace ArrayListSORTING
{

public enum SortDirection
{
Asc,
Desc
}

public class CategoryComparer : IComparer
{

private SortDirection m_direction = SortDirection.Asc;

public CategoryComparer() : base() { }

public CategoryComparer(SortDirection direction)
{
this.m_direction = direction;
}

int IComparer.Compare(object x, object y)
{

Category categoryX = (Category) x;
Category categoryY = (Category) y;

if (categoryX == null && categoryY == null)
{
return 0;
}
else if (categoryX == null && categoryY != null)
{
return (this.m_direction == SortDirection.Asc) ? -1 : 1;
}
else if (categoryX != null && categoryY == null)
{
return (this.m_direction == SortDirection.Asc) ? 1 : -1;
}
else
{
return (this.m_direction == SortDirection.Asc) ?
categoryX.TypeCode.CompareTo(categoryY.TypeCode) :
categoryY.TypeCode.CompareTo(categoryX.TypeCode);
}
}
}
}


Testing With Sample Code



For testing our sample we need to create a new arraylist and we have to add some items in that arraylist and test it.


using System;
using System.Collections;

namespace ArrayListSORTING
{

class SortArrayList
{

[STAThread]
static void Main(string[] args)
{

ArrayList list = new ArrayList();

list.Add(new Category("Music", 4));
list.Add(new Category("Play", 7));
list.Add(new Category("Dance", 2));
list.Add(new Category("Party", 9));
list.Add(new Category("Disco", 1));
list.Add(new Category("Sport", 3));

list.Sort(new CategoryComparer());

Console.WriteLine("Array in Ascending Order");
foreach (Category cate in list)
{
Console.WriteLine("CategoryName: " + cate.Type + ", CategoryCode: " + cate.TypeCode.ToString());
}

list.Sort(new CategoryComparer(SortDirection.Desc));

Console.WriteLine("\n\rArray in Desending Order");
foreach (Category cate in list)
{
Console.WriteLine("CategoryName: " + cate.Type + ", CategoryCode: " + cate.TypeCode.ToString());
}
Console.ReadLine();
}
}
}





Tryout this sample with console application. And you can implement where ever you want, in your application.




Responses

Author: Tomas Thalmann    11 May 2009Member Level: Bronze   Points : 1
great way to use IComparer, but wouldn't it be easier to only use IComparable?

http://ttprog.wordpress.com/2009/04/20/sorting-arraylists-in-c/


Author: greeny_1984    11 May 2009Member Level: Diamond   Points : 1
Hi,

Nice piece of information

thanks for sharing with us

regards,

greeny_1984


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Sorting Arraylists  .  Sorting an Arraylist in C#  .  Sorting an ArrayList  .  Sort an ArrayList in C#  .  Sort an ArrayList  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Effective use of Dataset
Previous Resource: The thing that happens behind the scenes!!!
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
Related Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use