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.Loon, Size = 14 }, new Duck() { Kind = KindOfDuck.Muscovy, Size = 11 }, new Duck() { Kind = KindOfDuck.Mallard, Size = 14 }, new Duck() { Kind = KindOfDuck.Loon, Size = 13 }, }; IComparer <Duck> kindComparer = new DuckComparerByKind(); ducks.Sort(kindComparer); PrintDucks(ducks); DuckComparer comparer = new DuckComparer(); Console.WriteLine("\nSorting by kind then size\n"); comparer.SortBy = SortCriteria.KindThenSize; ducks.Sort(comparer); PrintDucks(ducks); Console.WriteLine("\nSorting by size then kind\n"); comparer.SortBy = SortCriteria.SizeThenKind; ducks.Sort(comparer); PrintDucks(ducks); }
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.Mallard, Size = 11 }, new Duck() { Kind = KindOfDuck.Muscovy, Size = 14 }, new Duck() { Kind = KindOfDuck.Decoy, Size = 13 } }; Console.WriteLine("Ducks compared within class by size"); ducks.Sort(); PrintDucks(ducks); Console.WriteLine("Ducks compared with comparer class by size"); DuckComparerBySize sizeComparer = new DuckComparerBySize(); ducks.Sort(sizeComparer); PrintDucks(ducks); Console.WriteLine("Ducks compared with comparer class by kind"); DuckComparerByKind kindComparer = new DuckComparerByKind(); ducks.Sort(kindComparer); PrintDucks(ducks); DuckComparer comparer = new DuckComparer(); Console.WriteLine("Ducks compared with enum comparer class by kind then size"); comparer.SortBy = SortCriteria.KindThenSize; ducks.Sort(comparer); PrintDucks(ducks); Console.WriteLine("Ducks compared with enum comparer class by size then kind"); comparer.SortBy = SortCriteria.SizeThenKind; ducks.Sort(comparer); PrintDucks(ducks); Console.ReadKey(); }
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 }, }; Console.WriteLine("--Original duck list"); PrintDucks(ducks); //IComparable sort Console.WriteLine("\r\n--IComparable Sort - size descending"); ducks.Sort(); PrintDucks(ducks); //IComparer sort Console.WriteLine("\r\n--IComparer Sort - size ascending"); DuckCompareBySize sizeComparer = new DuckCompareBySize(); ducks.Sort(sizeComparer); PrintDucks(ducks); Console.WriteLine("\r\n--IComparer Sort - Kind ascending by enum index"); DuckCompareByKind kindComparer = new DuckCompareByKind(); ducks.Sort(kindComparer); PrintDucks(ducks); Console.WriteLine("\r\n--IComparer Complex Sort - Size/Kind ascending "); DuckComparer comparer = new DuckComparer(); comparer.SortBy = SortCriteria.SizeThenKind; ducks.Sort(comparer); PrintDucks(ducks); Console.WriteLine("\r\n--IComparer Complex Sort - Kind/Size ascending "); //DuckComparer comparer = new DuckComparer(); comparer.SortBy = SortCriteria.KindThenSize; ducks.Sort(comparer); PrintDucks(ducks); //IEnumerator Duck[] duckArray = new Duck[6]; duckArray.GetEnumerator(); Console.WriteLine("\r\n--IEnumerator loop "); IEnumerator <Duck> enumerator = ducks.GetEnumerator(); while (enumerator.MoveNext()) { Duck duck = enumerator.Current; Console.WriteLine(duck); } }