IStructuralComparable Interface


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Supports the structural comparison of collection objects.

Namespace:   System.Collections
Assembly:  mscorlib (in mscorlib.dll)

type IStructuralComparable = interface end

System_CAPS_pubmethodCompareTo(Object, IComparer)

Determines whether the current collection object precedes, occurs in the same position as, or follows another object in the sort order.

The IStructuralComparable interface enables you to implement customized comparisons for collection members. That is, you can define precisely what it means for one collection object to precede, follow, or occur in the same position in the sort order as a second collection object. You can then specify that this definition be used with a collection type that accepts the IStructuralComparable interface.

The interface has a single member, CompareTo, which determines whether the current collection object is less than, equal to, or greater than a second object in the sort order. The actual comparison of the members or elements in the current instance with those in a second object is performed by an IComparer interface implementation, which contains the definition of your custom comparison.


The IStructuralComparable interface supports only structural comparisons for sorting or ordering. The IStructuralEquatable interface supports custom comparisons for structural equality.

The .NET Framework provides two default comparers. One is returned by the StructuralComparisons.StructuralComparer property; the other is returned by the Comparer<'T>.Default property.

The generic tuple classes (Tuple<'T1>, Tuple<'T1, 'T2>, Tuple<'T1, 'T2, 'T3>, and so on) and the Array class provide explicit implementations of the IStructuralComparable interface. By casting (in C#) or converting (in Visual Basic) the current instance of an array or tuple to an IStructuralComparable interface value and providing your IComparer implementation as an argument to the CompareTo method, you can define a custom sort order for the array or collection. However, you do not call the CompareTo method directly in most cases. Instead, the CompareTo method is called by sorting methods such as Sort(Array, IComparer). In this case, you define your IComparer implementation and pass it as an argument to a sorting method or collection object's class constructor. The CompareTo method with your custom comparer is then called automatically whenever the collection is sorted.

The following example creates an array of Tuple<'T1, 'T2, 'T3, 'T4, 'T5, 'T6> objects that contains population data for three U.S. cities from 1960 to 2000. The sextuple's first component is the city name. The remaining five components represent the population at ten-year intervals from 1960 to 2000.

The PopulationComparer class provides an IComparer implementation that allows the array of sextuples to be sorted by any one of its components. Two values are provided to the PopulationComparer class in its constructor: the position of the component that defines the sort order, and a Boolean value that indicates whether the tuple objects should be sorted in ascending or descending order.

The example then displays the elements in the array in unsorted order, sorts them by the third component (the population in 1970) and displays them, and then sorts them by the sixth component (the population in 2000) and displays them. Note that the example does not directly call the CompareTo method. The method is called implicitly by the Sort(Array, IComparer) method for each tuple object in the array.

No code example is currently available or this language may not be supported.

Universal Windows Platform
Available since 8
.NET Framework
Available since 4.0
Portable Class Library
Supported in: portable .NET platforms
Available since 4.0
Windows Phone Silverlight
Available since 8.0
Windows Phone
Available since 8.1
Return to top