Sorted Set Examples

Sorted sets are one of the most overlooked collection types. In short this collection can maintain elements in an order. Read more for the examples and an explanation.

SortedSetSolution SortedSetProgram

 

MyCustomComparer.cs Code Contents

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MySets
{
    public class MyCustomComparer : IComparer<string>
    {
        public int Compare(string x, string y)
        {
            return String.Compare(x, y, StringComparison.InvariantCultureIgnoreCase);
        }
    }
}

This is a simple string comparer class that I’m going to use in the Program.cs class. I’ll use this when I’m choosing to defer from the usual .NET comparers.

Program.cs Code Contents

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MySets
{
    class Program
    {
        /// <summary>
        /// This program demonstrates how SortedSets work.
        /// This is done without any string comparers, with the built in string comparer,
        /// and with a custom string comparer.
        /// </summary>
        /// <param name="args"></param>

        static void Main(string[] args)
        {
            // Displaying the sorted set without a comparer. Notice how the collection is 
            // displayed in alphabetical order.
            // Also notice that lowercase Los Angeles is only displayed once but the 
            // all caps LOS ANGELES is added.
            Console.WriteLine("****Sorted Set Without A String Comparer****");
            var sortedSetNoComparer = hashsetWithNoComparer();
            foreach (var city in sortedSetNoComparer)
                Console.WriteLine(city);
            Console.WriteLine();

            // Displaying the sorted set with the .NET string comparer. This will make sure
            // that no matter the case the string is listed only once.
            Console.WriteLine("****Sorted Set With A String Comparer****");
            var sortedSetWithComparer = hashsetWithComparer();
            foreach (var city in sortedSetWithComparer)
                Console.WriteLine(city);
            Console.WriteLine();

            // Displaying the sorted set with a custom string comparer. This will perform the same
            // operations as the built in comparer. You just have a class that you can customize.
            Console.WriteLine("****Sorted Set With A Custom String Comparer");
            var sortedSetWithCustomComparer = hashsetWithCustomComparer();
            foreach (var city in sortedSetWithCustomComparer)
                Console.WriteLine(city);
            Console.WriteLine();

            Console.ReadLine();
        }

        // Private Methods
        //
        // Method that creates a sorted set without any string comparer.
        private static SortedSet<string> hashsetWithNoComparer()
        {
            SortedSet<string> cities = new SortedSet<string>
            {
                "New York", "Manchester", "Los Angeles", "Paris"
            };

            cities.Add("Los Angeles");
            cities.Add("Beijing");
            cities.Add("LOS ANGELES");
            return cities;
        }

        // Method that creates a sorted set with a built in string comparer.
        private static SortedSet<string> hashsetWithComparer()
        {
            SortedSet<string> cities = new SortedSet<string>(StringComparer.InvariantCultureIgnoreCase)
            {
                "New York", "Manchester", "Los Angeles", "Paris"
            };

            cities.Add("Los Angeles");
            cities.Add("Beijing");
            cities.Add("LOS ANGELES");
            return cities;
        }

        // Method that creates a sorted set using a custom string comparer class.
        private static SortedSet<string> hashsetWithCustomComparer()
        {
            SortedSet<string> cities = new SortedSet<string>(new MyCustomComparer())
            {
                "New York", "Manchester", "Los Angeles", "Paris"
            };

            cities.Add("Los Angeles");
            cities.Add("Beijing");
            cities.Add("LOS ANGELES");
            return cities;
        }
    }
}

Here I’m showing a couple things at the same time with sorted sets. I’m creating three sorted sets of string values, they are adding elements after the set creation, and two of them are using string comparers to ensure that only unique elements are added to the collection. A sorted set is good if you need the collections to be in some sort of order regardless of when they have been added to the collection. This is being shown by the Add methods being called and adding elements to an existing set.

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s