Exercises in this lecture   previous -- Keyboard shortcut: 'p'        Go to the notes, in which this exercise belongs -- Keyboard shortcut: 'u'   Alphabetic index   Course home      

Exercise 8.7
Comparable Dice

In this exercise we will arrange that two dice can be compared to each other. The result of die1.CompareTo(die2) is an integer. If the integer is negative, die1 is considered less than die2; If zero, die1 is considered equal to die2; And if positive, die1 is considered greater than die2. When two dice can be compared to each other, it is possible sort an array of dice with the standard Sort method in C#.

Program a version of class Die which implements the interface System.IComparable.

Consult the documentation of the (overloaded) static method System.Array.Sort and locate the Sort method which relies on IComparable elements.

Make an array of dice and sort them by use of the Sort method.