O'Reilly logo

Microsoft® .NET Compact Framework (Core Reference) by Mark Sutton, Rory MacLoed, Robert Burbidge, Stephen Wheelwright, Andy Wigley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Sorting Objects in Collections

When you have selected the appropriate collection class for your application, you might still need to add functionality to the objects in the collection to achieve the desired results. For example, the Array and ArrayList classes give you the Sort method to sort objects into some order. However, when you call the Sort method, a run-time exception is thrown if the class of the elements in the collection does not support the IComparable interface.

Sorting with IComparable

If you want to support only a single sort order for your objects, you can implement the IComparable interface, which consists of a single method, CompareTo. When sorting, the system calls this method for a particular object in the collection to compare ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required