Exemplo n.º 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();

        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            //SORT BY SIZE----------------------------------------
            Console.WriteLine("Ducks:");

            foreach (Duck d in ducks)
            {
                d.Print();
            }

            ducks.Sort();
            //or
            ducks.Sort(new DuckComparerBySize());

            Console.WriteLine("\nSorted ducks:");

            foreach (Duck d in ducks)
            {
                d.Print();
            }

            //SORT BY KIND----------------------------------------

            ducks.Sort(new DuckComparerByKind());

            Console.WriteLine("\nSorted by kind:");

            foreach (Duck d in ducks)
            {
                d.Print();
            }

            //SORT BY KIND THEN SIZE

            var duckComparer = new DuckComparer {
                SortBy = SortCriteria.KindThenSize
            };

            ducks.Sort(duckComparer);

            Console.WriteLine("\nSorted by kind then size:");
            foreach (Duck d in ducks)
            {
                d.Print();
            }

            //SORT BY KIND THEN SIZE

            duckComparer.SortBy = SortCriteria.SizeThenKind;
            ducks.Sort(duckComparer);

            Console.WriteLine("\nSorted by size then kind:");
            foreach (Duck d in ducks)
            {
                d.Print();
            }

            Console.ReadKey();
        }