Example #1
0
        static void Main(string[] args)
        {
        

            List<Duck> ducks = new List<Duck>()
            {
                new Duck() { Kind = KindOfDuck.Mallard, Size = 17 },
                new Duck() { Kind = KindOfDuck.Muscovy, Size = 18 },
                new Duck() { Kind = KindOfDuck.Decoy, Size = 14 },
                new Duck() { Kind = KindOfDuck.Muscovy, Size = 11 },
                new Duck() { Kind = KindOfDuck.Mallard, Size = 14 },
                new Duck() { Kind = KindOfDuck.Decoy, Size = 13 },
            };
            DuckComparerBySize sizeComparer = new DuckComparerBySize();
            ducks.Sort(sizeComparer);
            PrintDucks(ducks);

            DuckComparerByKind kindComparer = new DuckComparerByKind();
            ducks.Sort(kindComparer);
            PrintDucks(ducks);

            DuckComparer comparer = new DuckComparer();

            comparer.SortBy = SortCriteria.KindThenSize;
            ducks.Sort(comparer);
            PrintDucks(ducks);

            comparer.SortBy = SortCriteria.SizeThenKind;
            ducks.Sort(comparer);
            PrintDucks(ducks);

            Console.ReadKey();

        }
Example #2
0
        static void Main(string[] args)
        {
            List <Duck> ducks = new List <Duck>()
            {
                new Duck()
                {
                    Kind = KindOfDuck.Mallard, Size = 17
                },
                new Duck()
                {
                    Kind = KindOfDuck.Muscovy, Size = 18
                },
                new Duck()
                {
                    Kind = KindOfDuck.Decoy, Size = 14
                },
                new Duck()
                {
                    Kind = KindOfDuck.Muscovy, Size = 11
                },
                new Duck()
                {
                    Kind = KindOfDuck.Mallard, Size = 14
                },
                new Duck()
                {
                    Kind = KindOfDuck.Decoy, Size = 13
                },
            };

            //ducks.Sort();

            DuckComparerBySize compareBySize = new DuckComparerBySize();
            DuckComparerByKind compareByKind = new DuckComparerByKind();

            ducks.Sort(compareBySize);
            Console.WriteLine("Sort by size:");
            PrintDucks(ducks);

            ducks.Sort(compareByKind);
            Console.WriteLine("Sort by kind:");
            PrintDucks(ducks);

            Console.ReadKey();
        }