SortedSet<T> Constructor (IComparer<T>)

.NET Framework (current version)
 

Initializes a new instance of the SortedSet<T> class that uses a specified comparer.

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

public SortedSet(
	IComparer<T> comparer
)

Parameters

comparer
Type: System.Collections.Generic.IComparer<T>

The default comparer to use for comparing objects.

Exception Condition
ArgumentNullException

comparer is null.

If comparer does not implement IComparable<T>, you must specify an IComparer<T> object to be used.

The following example defines a comparer (ByFileExtension) that is used to construct a sorted set that sorts file names by their extensions. This code example is part of a larger example provided for the SortedSet<T> class.

// Create a sorted set using the ByFileExtension comparer.
SortedSet<string> mediaFiles1 =
    new SortedSet<string>(new ByFileExtension());
// Defines a comparer to create a sorted set
// that is sorted by the file extensions.
public class ByFileExtension : IComparer<string>
{
    string xExt, yExt;

	CaseInsensitiveComparer caseiComp = new CaseInsensitiveComparer();

    public int Compare(string x, string y)
    {
        // Parse the extension from the file name. 
        xExt = x.Substring(x.LastIndexOf(".") + 1);
        yExt = y.Substring(y.LastIndexOf(".") + 1);

        // Compare the file extensions. 
        int vExt = caseiComp.Compare(xExt, yExt);
        if (vExt != 0)
        {
            return vExt;
        }
        else
        {
            // The extension is the same, 
            // so compare the filenames. 
            return caseiComp.Compare(x, y);
        }
    }
}

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