Click here to Skip to main content
15,880,503 members
Articles / Programming Languages / C#

Automatic sorted list

Rate me:
Please Sign up or sign in to vote.
4.88/5 (21 votes)
21 Apr 20035 min read 118.6K   1.2K   51  
Here is an advanced ArrayList which uses IComparable or IComparer interface to sort its objects and which provides some other useful functions such as duplicates limitation.
using System;
using System.Collections;

using EMK.Collections;


namespace EMK.Tests
{
	/// <summary>
	/// Simple test for the SortableList class.
	/// Shows the way to use the basic properties and methods.
	/// </summary>
	public class TestSortedList
	{
		/// <summary>
		/// Icomparer to provide to the SortableList constructor
		/// </summary>
		class AntiAlphabeticalComparer: IComparer
		{
			public int Compare(object O1, object O2)
			{
				string S1 = O1.ToString();
				string S2 = O2.ToString();
				return -String.Compare(S1, S2);
			}
		}

		/// <summary>
		/// Entry point for the SortableList use case.
		/// </summary>
		public static void Main()
		{
			Console.WriteLine("You create a new SortableList." );
			SortableList SL = new SortableList();

			Console.Write("You set the KeepSorted property to false and you fill it with the strings X, B, A, D: " );
			SL.KeepSorted = false;
			SL.Add("X");
			SL.Add("B");
			SL.Add("A");
			SL.Add("D");
			Console.WriteLine(SL);

			Console.Write("You can insert or set elements where you want since KeepSorted==false. Let's set 'C' to index 4: ");
			SL[3] = "C";
			Console.WriteLine(SL);

			Console.Write("You decide to sort the list: ");
			SL.Sort();
			Console.WriteLine(SL);

			Console.Write("You now set the KeepSorted property to true and add some new strings: ");
			SL.KeepSorted = true;
			SL.Add("J");
			SL.Add("E");
			SL.Add("E");
			SL.Add("B");
			SL.Add("X");
			SL.Add("E");
			SL.Add("E");
			Console.WriteLine(SL);

			Console.WriteLine("'E' is found at index "+SL.IndexOf("E").ToString());
			Console.WriteLine("Is the list containing an 'X' value ?: "+SL.Contains("X").ToString());
			Console.WriteLine("Is the list containing an 'M' value ?: "+SL.Contains("M").ToString());

			Console.Write("You limit the number of occurrences of 'E' to 2: ");
			SL.LimitNbOccurrences("E", 2);
			Console.WriteLine(SL);

			Console.Write("After all you do not want any duplicates: ");
			SL.RemoveDuplicates();
			Console.WriteLine(SL);

			Console.Write("You set the AddDuplicates property to false and try to add J and E again: ");
			SL.AddDuplicates = false;
			SL.Add("J");
			SL.Add("E");
			Console.WriteLine(SL);

			Console.WriteLine("Now you create another SortableList but this time you give it an IComparer class which is the anti-alphabetical order." );
			SL = new SortableList( new AntiAlphabeticalComparer() );

			Console.Write("You fill the list by adding a range of vowels in alphabetical order. Result: " );
			string[] Vowels = new string[] {"A", "E", "I", "O", "U"};
			SL.AddRange(Vowels);
			Console.WriteLine(SL);
			Console.ReadLine();
		}		
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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
Web Developer
France France
Eric Marchesin is working as a software development engineer at Dassault Systèmes, Paris. Dassault Systèmes is a global leader in the market for Product Lifecycle Management using 3D modeling digital technology.
He has also been working for Video Game companies as well as Artificial Intelligence projects.
His programming experience more specifically includes C/C++, MFC, OpenGL, C# and .NET framework.

Of course I appreciate beautiful and fine algorithms. There's quite an art to creating powerful, effective and ergonomic programs.
Among many other things I enjoy music, sun, passion and generally whatever makes you imagine, travel and dream.

Comments and Discussions