SortedSet<T> Constructor (IComparer<T>)

.NET Framework 4.6 and 4.5

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

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

public SortedSet(
	IComparer<T> comparer


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

The default comparer to use for comparing objects.


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;
            // The extension is the same,  
            // so compare the filenames.  
            return caseiComp.Compare(x, y);

.NET Framework

Supported in: 4.6, 4.5, 4

.NET Framework Client Profile

Supported in: 4

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Portable Class Library

Supported in: Portable Class Library
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
© 2015 Microsoft