SortedSet<T> Constructor (IComparer<T>)
Initializes a new instance of the SortedSet<T> class that uses a specified comparer.
Assembly: System (in System.dll)
| 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); } } }
Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.