Sorts an IList collections - CSharp System.Collections.Generic

CSharp examples for System.Collections.Generic:IList

Description

Sorts an IList collections

Demo Code


using System.IO;/*w  w  w.  java  2  s .c  o  m*/
using System;

public class Main{
        /// <summary>
        /// Sorts an IList collections
        /// </summary>
        /// <param name="list">The System.Collections.IList instance that will be sorted</param>
        /// <param name="Comparator">The Comparator criteria, null to use natural comparator.</param>
        public static void Sort(System.Collections.IList list, System.Collections.IComparer Comparator)
        {
            if (((System.Collections.ArrayList)list).IsReadOnly)
                throw new NotSupportedException();

            if ((Comparator == null) || (Comparator is System.Collections.Comparer))
            {
                try
                {
                    ((System.Collections.ArrayList)list).Sort();
                }
                catch (InvalidOperationException e)
                {
                    throw new InvalidCastException(e.Message);
                }
            }
            else
            {
                try
                {
                    ((System.Collections.ArrayList)list).Sort(Comparator);
                }
                catch (InvalidOperationException e)
                {
                    throw new InvalidCastException(e.Message);
                }
            }
        }
}

Related Tutorials