- Comparison delegates allow animals to be sorted by age as well as the existing values.
Make sure the zoo only starts with these animals and guests.
// Animals for sorting
this.AddAnimal(new Chimpanzee("Bobo", 10, 128.2, Gender.Male));
this.AddAnimal(new Chimpanzee("Bubbles", 3, 103.8, Gender.Female));
this.AddAnimal(new Dingo("Spot", 5, 41.3, Gender.Male));
this.AddAnimal(new Dingo("Maggie", 6, 37.2, Gender.Female));
this.AddAnimal(new Dingo("Toby", 0, 15.0, Gender.Male));
this.AddAnimal(new Eagle("Ari", 12, 10.1, Gender.Female));
this.AddAnimal(new Hummingbird("Buzz", 2, 0.02, Gender.Male));
this.AddAnimal(new Hummingbird("Bitsy", 1, 0.03, Gender.Female));
this.AddAnimal(new Kangaroo("Kanga", 8, 72.0, Gender.Female));
this.AddAnimal(new Kangaroo("Roo", 0, 23.9, Gender.Male));
this.AddAnimal(new Kangaroo("Jake", 9, 153.5, Gender.Male));
this.AddAnimal(new Ostrich("Stretch", 26, 231.7, Gender.Male));
this.AddAnimal(new Ostrich("Speedy", 30, 213.0, Gender.Female));
this.AddAnimal(new Platypus("Patti", 13, 4.4, Gender.Female));
this.AddAnimal(new Platypus("Bill", 11, 4.9, Gender.Male));
this.AddAnimal(new Platypus("Ted", 0, 1.1, Gender.Male));
this.AddAnimal(new Shark("Bruce", 19, 810.6, Gender.Female));
this.AddAnimal(new Shark("Anchor", 17, 458.0, Gender.Male));
this.AddAnimal(new Shark("Chum", 14, 377.3, Gender.Male));
this.AddAnimal(new Squirrel("Chip", 4, 1.0, Gender.Male));
this.AddAnimal(new Squirrel("Dale", 4, 0.9, Gender.Male));
// Guests for sorting
this.AddGuest(new Guest("Greg", 35, 100.0m, WalletColor.Crimson, Gender.Male, new Account()), new Ticket(0m, 0, 0));
this.AddGuest(new Guest("Darla", 7, 10.0m, WalletColor.Brown, Gender.Female, new Account()), new Ticket(0m, 0, 0));
this.AddGuest(new Guest("Anna", 8, 12.56m, WalletColor.Brown, Gender.Female, new Account()), new Ticket(0m, 0, 0));
this.AddGuest(new Guest("Matthew", 42, 10.0m, WalletColor.Brown, Gender.Male, new Account()), new Ticket(0m, 0, 0));
this.AddGuest(new Guest("Doug", 7, 11.10m, WalletColor.Brown, Gender.Male, new Account()), new Ticket(0m, 0, 0));
this.AddGuest(new Guest("Jared", 17, 31.70m, WalletColor.Brown, Gender.Male, new Account()), new Ticket(0m, 0, 0));
this.AddGuest(new Guest("Sean", 34, 20.50m, WalletColor.Brown, Gender.Male, new Account()), new Ticket(0m, 0, 0));
this.AddGuest(new Guest("Sally", 52, 134.20m, WalletColor.Brown, Gender.Female, new Account()), new Ticket(0m, 0, 0));Currently when we sort we are always calling the animal sort, but now that we have guests to sort we will have to add another parameter to test against. We will be implementing it later on.
Program class, create an if-statement that compares commandWords[1] to "animals".SortAnimals into the body of the if statement.sortResult variable before the if statement to hold a SortResult object. Assign it to null.Change the index arguments based on the structure: "sort animals bubble weight". The result should look like the following when it is written to the console:
SORT TYPE: BUBBLE
SORT BY: ANIMALS
SORT VALUE: WEIGHT
SWAP COUNT: 108
COMPARE COUNT: 210Zoo class as specified in the diagram below.
NameSortComparer method, return the result of calling the string.Compare method.Pass in the names of the two animals.
WeightSortComparer, use the list below to determine the value to return.double weight1 = animal1.Weight;
double weight2 = animal2.Weight;
return weight1 == weight2 ? 0 : weight1 > weight2 ? 1 : -1;SortAnimals method.Func<Animal, Animal, int> named compareFunc.If the sortValue is "name", set the variable to the NameSortComparer method. Otherwise, set the variable to the WeightSortComparer method.
SortHelper class.The difference between each pair of Sort methods is what to compare (either weight or name) and how to compare them (either using the string compare method for names or the comparison operators for weight). The methods' algorithms are identical otherwise. Now that we have moved the logic of what to compare and how to compare them into their own methods (the Name and Weight sort comparer methods), we can reduce each pair of Sort algorithms to 1 and take in a comparing method as a parameter. This allows us to abide by DRY while still sorting by different values! It also allows us to easily make new ways to compare things while still using the same Sorting algorithms!

Func parameter as shown in the class diagram.
if (comparer(animals[j], animals[j + 1]) > 0)
{
// swap code and swapCounter
}Sort method for that type.Zoo class, create an AgeSortComparer modeled after the WeightSortCompare method.Zoo's SortAnimal method, add a condition for using the AgeSortComparer.Sort animals by name and by weight using each sort type.The command should be modeled after "sort animals bubble weight". Ensure that the animals are sorted correctly each time.
Sort animals by age using each type.Ensure that the animals are sorted correctly each time. Restart the zoo between sorts for better results.
commandWords array to "guests".Leave it empty for now.
Zoo and SortHelper classes as specified in the diagram below.These changes - changing animals to objects - will make the sort methods generic so that they can be used to sort any collection of objects that supports the IList interface.
'Object' and 'object'. Make sure when changing the types that you are using the 'object' type.System.Collection to use the IList interface.
Zoo's existing Comparer methods, cast the objects to an Animal type.NameSortComparer to AnimalNameSortComparer and update references to it.SortAnimals method, change the compareFunc variable to be of type Func<object, object, int>Zoo class, create a GuestNameSortComparer method and model it after the existing NameSortComparer method.MoneyBalanceSortComparer method modeled after the WeightSortComparer.Have the method accept two objects and compare the sum of their Wallet and CheckingAccount money balances.
Sort methods, you will have to cast the resulting SortResult's object initializer on the Objects property in order to get the code to compile.To do so, set the Objects to the code below:
list.Cast<object>().ToList()Zoo class, rename the SortAnimals method to be SortObjects. In the methodQuickSort, pass in the count of the list parameter - 1 instead of the count of the zoo's animals.SortAnimals and SortGuests methods in the Zoo class.Within these methods call the SortObjects method and pass in the appropriate list from the zoo.
SortObjects method, ensure there are conditions for the sortValue of: animalname, guestname, animalage, animalweight, and moneybalance.Use them to assign the correct sort comparer.
"sort" case in the Program classSortAnimals or SortGuests depending on commandWords[1].M. Modify the binary search:
SortAnimals, change the "name" argument to "animalname"SortAnimals, define a new variable of type List<Animal> and assign it to the sortResult's Objects property.Use "sort guests selection guestName" as a template.
Predicate parameters allow the find operations to use different matching conditions.Zoo class as specified in the diagram below.
FindAnimal and FindGuest methods, return the result of calling the Find extension method on the appropriate list.Pass the match Predicate parameter through.
FindAnimal and FindGuest pass in a lambda expression that specifies a condition for which object to find.For example, if you want to find an animal with a specified name, your lambda expression might look like the code below:
6.4.3 How to Use Lambda ExpressionsAnimal animal = zoo.FindAnimal(a => a.Name == name);MainWindow's adoptAnimalButton_Click event handler to pass a predicate into the FindGuest method rather than using a selected guest.Then show that animal (the show command uses the FindAnimal method). Both commands should work as before.
Then show that guest (the show command uses the FindGuest method). Both commands should work as before.
Open the WPF application, select an animal, then click the adopt button. The first guest without an adopted animal in the list should adopt that animal. Show the cage to make sure the guest and the animals are all drawn.
Ensure you get a message about no available guests.
Use a different sorting algorithm each time you sort.
Use a different sorting algorithm each time you sort.
© 2026 Northcentral Technical College