
Note that there are many ways to do this sort of thing. For example, Lesson 37
explains how you can use LINQ to sort items. As with all of the examples and
exercises in this book, these examples are primarily designed to demonstrate par-
ticular topics, in this case interfaces, rather than to provide the perfect solution.
3. If you set a ListView control’s ListViewItemSorter property equal to an object that imple-
ments the
System.Collections.IComparer interface, then the ListView uses that object to
sort its rows. To sort the rows, the control calls the object’s
Compare method, passing it two
ListViewItem objects. (Unfortunately the ListView control’s ListViewItemSorter prop-
erty is a non-generic
IComparer, so it works with non-specific objects instead of something
more concrete like
ListViewItems.)
For this exercise, make a program with a
ListView control similar to the one shown in
Figure 27-6. At design time, edit the
ListView’s Columns collection to define the columns.
Edit its
Items collection to define the data and set the control’s View property to Details.
FIGURE 276
Next make a ListViewComparer class that implements System.Collections.IComparer.
Give it a
ColumnNumber property that indicates the number of the column in the ListView
that the object should use when sorting.
Finally give the
ListView a ColumnClick event handler. The event handler should create ...