Click or drag to resize

RankedSetT Constructor (IComparerT)

Initializes a new set instance that uses the supplied comparer.

Namespace:  Kaos.Collections
Assembly:  KaosCollections (in KaosCollections.dll) Version: 4.2.0.0
Syntax
C#
public RankedSet(
	IComparer<T> comparer
)

Parameters

comparer
Type: System.Collections.GenericIComparerT
The comparer to use for sorting items.
Exceptions
ExceptionCondition
InvalidOperationExceptionWhen comparer is null and no other comparer available.
Examples
This program shows usage of a custom comparer for case insensitivity.
C#
using System;
using Kaos.Collections;

namespace ExampleApp
{
    class RsExample01
    {
        static bool IsPolynymous (string name) => name.Contains (" ");

        static void Main()
        {
            var musicians = new RankedSet<string> (StringComparer.InvariantCultureIgnoreCase);

            foreach (var m1 in new string[] { "Falco", "k.d. lang", "Madonna", "Tom Petty",
                                              "Joni Mitchell", "Grimes", "Warren Zevon" })
                musicians.Add (m1);

            Console.WriteLine ("Candidates:");
            foreach (var item in musicians)
                Console.WriteLine ($"  {item}");

            musicians.Remove ("Falco");
            musicians.RemoveWhere (IsPolynymous);
            musicians.RemoveRange (1, musicians.Count-1);

            Console.WriteLine ("\nFavorite:");
            foreach (var item in musicians)
                Console.WriteLine ($"  {item}");
        }

        /* Output:

        Candidates:
          Falco
          Grimes
          Joni Mitchell
          k.d. lang
          Madonna
          Tom Petty
          Warren Zevon

        Favorite:
          Grimes

        */
    }
}
See Also