Click here to Skip to main content
15,895,799 members
Articles / Programming Languages / C#

A Simple and Generic sorting technique for your business object collection

Rate me:
Please Sign up or sign in to vote.
4.07/5 (10 votes)
19 Aug 20051 min read 69.5K   134   34  
This article discusses sorting of a user defined collection object based on any of the properties of the business entity.
using System;
using System.Collections;

namespace CollectionTest
{
	/// <summary>
	/// Student Class with 3 properties.
	/// </summary>
	public class Student
	{
		string name="";
		public string Name
		{
			get{return name;}
			set{name = value;}
		}
		int id=0;
		public int Id
		{
			get{return id;}
			set{id = value;}
		}
		DateTime dob=DateTime.MinValue;
		public DateTime DOB
		{
			get{return dob;}
			set{dob = value;}
		}
		public Student(string name,int id,DateTime Dob)
		{
			this.name = name;
			this.id = id;
			this.dob = Dob;
		}
	}//Student class

	/// <summary>
	/// Note : This student collection inherhits SortableCollectionBase
	/// In the constructor set the SortObjectType for sorting.
	/// </summary>
	public class StudentCollection : SortableCollectionBase
	{
		#region CollectionBase implementation

		public StudentCollection()
		{
			//In your collection class constructor add this line.
			//set the SortObjectType for sorting.
			base.SortObjectType = typeof(Student);
		}

		public Student this[ int index ]  
		{
			get  
			{
				return( (Student) List[index] );
			}
			set  
			{
				List[index] = value;
			}
		}

		public int Add( Student value )  
		{
			return( List.Add( value ) );
		}

		public int IndexOf( Student value )  
		{
			return( List.IndexOf( value ) );
		}

		public void Insert( int index, Student value )  
		{
			List.Insert( index, value );
		}

		public void Remove( Student value )  
		{
			List.Remove( value );
		}

		public bool Contains( Student value )  
		{
			// If value is not of type Int16, this will return false.
			return( List.Contains( value ) );
		}
		protected override void OnInsert( int index, Object value )  
		{
			// Insert additional code to be run only when inserting values.
		}
		protected override void OnRemove( int index, Object value )  
		{
			// Insert additional code to be run only when removing values.
		}
		protected override void OnSet( int index, Object oldValue, Object newValue )  
		{
			// Insert additional code to be run only when setting values.
		}
		protected override void OnValidate( Object value )  
		{
		}
		#endregion
		
	}

}

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
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions