using System;

namespace TestDelegate
{
    static class Util
    {
        public delegate int CompareFunction(Object first, Object second);

        public static void sort(CompareFunction f, Object[] data)
        {
            for (int i = 1; i < data.Length; i++)
            {
                Object current = data[i];
                int j = i;
                while (j > 0 && f(current, data[j - 1]) < 0)
                {
                    data[j] = data[j - 1];
                    j--;
                }
                data[j] = current;
            }
        }

        public static void print(Object[] data)
        {
            foreach (Object o in data)
            {
                Console.Write(o);
            }
            Console.WriteLine();
        }
    }
    
    class TestDelegate
    {
        // This function is, of course, terribly unsafe, and will

        // throw an InvalidCastException if either input does not

        // implement IComparable

        public static int increasing(Object first, Object second)
        {
            return ((IComparable) first).CompareTo((IComparable) second);
        }

        public static int decreasing(Object first, Object second)
        {
            return -1 * ((IComparable)first).CompareTo((IComparable)second);
        }

        static void Main(string[] args)
        {
            // Automatic boxing

            Object[] myArray = {4, 6, 1, 2, 9};

            Util.sort(new Util.CompareFunction(increasing), myArray);
            Util.print(myArray);
            Util.sort(new Util.CompareFunction(decreasing), myArray);
            Util.print(myArray);

            Console.ReadKey();
        }
    }
}