Sort Arraylist of Arraylist in C#

using System;
using System.Collections;

class Program
{
static void Main()
{
ArrayList arraylist1item = new ArrayList();
ArrayList arraylist2item = new ArrayList();
ArrayList arraylist3item = new ArrayList();
ArrayList AllArrayList = new ArrayList();

arraylist1item.Add(“E;BUA;A1”);
arraylist1item.Add(“E;BUA;B1”);

arraylist2item.Add(“F;CHA;B1”);
arraylist2item.Add(“F;CHA;A1”);
arraylist2item.Add(“F;CHA;F1”);
arraylist2item.Add(“F;CHA;D1”);
arraylist2item.Add(“F;CHA;G1”);

arraylist3item.Add(“E;BIA;A1”);
arraylist3item.Add(“E;BIA;A2”);
arraylist3item.Add(“E;BIA;A3”);

AllArrayList.Add(arraylist1item);
AllArrayList.Add(arraylist2item);
AllArrayList.Add(arraylist3item);

for(int i = 0; i < AllArrayList.Count – 1; i++)
{
for(int j = i + 1; j < AllArrayList.Count; j++)
{
ArrayList temp1 = (ArrayList)AllArrayList[i];
ArrayList temp2 = (ArrayList)AllArrayList[j];
if (temp1.Count > temp2.Count) Swap(temp1, temp2);
}
}

// check it worked

foreach(string s in arraylist1item) Console.WriteLine(s);
Console.WriteLine();
foreach(string s in arraylist2item) Console.WriteLine(s);
Console.WriteLine();
foreach(string s in arraylist3item) Console.WriteLine(s);
Console.ReadKey();
}

static void Swap (ArrayList list1, ArrayList list2)
{
ArrayList list3 = new ArrayList(list1);
list1.Clear();
foreach(string s in list2) list1.Add(s);
list2.Clear();
foreach(string s in list3) list2.Add(s);
}

}

http://www.c-sharpcorner.com/Forums/Thread/127192/sort-arraylist-of-arraylist-in-C-Sharp.aspx

Advertisements
By simplemsexchange Posted in C#

One comment on “Sort Arraylist of Arraylist in C#

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s