How to allow Duplicate Entries in SortedDictionary in C#

In C#, the SortedDictionary<TKey,TValue> class is used to represent the collection of key/value pairs.
This class defined under System.Collections.Generic namespace and as we all know that the keys in the SortedDictionary are always unique ,immutable and cannot be null.
So.How do we allow duplicate entries in the SortedDictionary if there’s a situation in which there’s a need for entries with the same key?

Imagine a scenario in which Keys are DateTime and values are UniqueIds.For every Key we should have uniqueId and if the Key are equal then the Id should also be equal .
In this case its important to note that the Keys can be duplicate so that we can compare the Ids too.

Here is default implementation with Error:

public static void Main(string[] args)
        {
            SortedDictionary<DateTime, string> noOfEntries = new SortedDictionary<DateTime, string>();

            DateTime dateTime = new DateTime(2021, 08, 11, 01, 32, 45);
            DateTime dateTime2 = new DateTime(2021, 08, 11, 01, 32, 45);
            DateTime dateTime3 = new DateTime(2021, 08, 11, 07, 32, 45);
            DateTime dateTime4 = new DateTime(2021, 08, 11, 05, 32, 45);
            DateTime dateTime5 = new DateTime(2021, 08, 11, 03, 32, 45);
            DateTime dateTime6 = new DateTime(2021, 08, 11, 11, 32, 45);
            DateTime dateTime7 = new DateTime(2021, 08, 11, 09, 32, 45);

            string eventId = "A121";
            string eventId2 = "A122";
            string eventId3 = "A123";
            string eventId4 = "A124";
            string eventId5 = "A125";
            string eventId6 = "A126";
            string eventId7 = "A127";

            noOfEntries.Add(dateTime,eventId);
            noOfEntries.Add(dateTime2,eventId2);
            noOfEntries.Add(dateTime3,eventId3);
            noOfEntries.Add(dateTime4,eventId4);
            noOfEntries.Add(dateTime5,eventId5);
            noOfEntries.Add(dateTime6,eventId6);
            noOfEntries.Add(dateTime7,eventId7);


            for(int i=0;i<7;i++)
            {
                Console.WriteLine(noOfEntries.Keys.ElementAt(i) + "    " + noOfEntries.Values.ElementAt(i));
            }
            Console.ReadLine();
	    }

You will get the following error:

The Solution for this is to override the Compare Function of SortedDictinary with our own Function so that it allows the duplicate Keys. The Code is given Below:

namespace ConsoleApp1
{
    public class Program
    {
        public class DuplicateKeyComparator<TKey> : IComparer<TKey> where TKey : IComparable
        {
            public int Compare(TKey x, TKey y)
            {
                int result = x.CompareTo(y);
                if (result == 0)
                {
                    return 1;
                }
                else
                {
                    return result;
                }
            }
        }


        public static void Main(string[] args)
        {
            SortedDictionary<DateTime, string> noOfEntries = new SortedDictionary<DateTime, string>(new DuplicateKeyComparator<DateTime>());

            DateTime dateTime = new DateTime(2021, 08, 11, 01, 32, 45);
            DateTime dateTime2 = new DateTime(2021, 08, 11, 01, 32, 45);
            DateTime dateTime3 = new DateTime(2021, 08, 11, 07, 32, 45);
            DateTime dateTime4 = new DateTime(2021, 08, 11, 05, 32, 45);
            DateTime dateTime5 = new DateTime(2021, 08, 11, 03, 32, 45);
            DateTime dateTime6 = new DateTime(2021, 08, 11, 11, 32, 45);
            DateTime dateTime7 = new DateTime(2021, 08, 11, 09, 32, 45);

            string eventId = "A121";
            string eventId2 = "A122";
            string eventId3 = "A123";
            string eventId4 = "A124";
            string eventId5 = "A125";
            string eventId6 = "A126";
            string eventId7 = "A127";

            noOfEntries.Add(dateTime,eventId);
            noOfEntries.Add(dateTime2,eventId2);
            noOfEntries.Add(dateTime3,eventId3);
            noOfEntries.Add(dateTime4,eventId4);
            noOfEntries.Add(dateTime5,eventId5);
            noOfEntries.Add(dateTime6,eventId6);
            noOfEntries.Add(dateTime7,eventId7);


            for(int i=0;i<7;i++)
            {
                Console.WriteLine(noOfEntries.Keys.ElementAt(i) + "    " + noOfEntries.Values.ElementAt(i));
            }
            Console.ReadLine();
        }
    }
}

Now if you run the code you will get the sorted Output with Duplicate Keys as expected.

Thanks for reading 🙂