Creating sorted sets with C# .NET
June 30, 2015 Leave a comment
The SortedSet of T object is the sorted version of the HashSet object. We’ve already seen what a HashSet can do for you in the referenced post. A SortedSet keeps the elements in increasing order.
Consider the following integer set:
SortedSet<int> sortedInts = new SortedSet<int>(); sortedInts.Add(1); sortedInts.Add(4); sortedInts.Add(3); sortedInts.Add(1); sortedInts.Add(3); sortedInts.Add(10); sortedInts.Add(8); sortedInts.Add(3); sortedInts.Add(1); sortedInts.Add(4); foreach (int i in sortedInts) { Debug.WriteLine(i); }
This will print…
1
3
4
8
10
Notice that duplicates were rejected to ensure uniqueness just like in the case of HashSets.
That is straightforward for primitive types like integers since .NET “knows” how to compare them. It can decide whether 10 is greater than 5, we don’t need to provide any help.
However what about reference types like your own objects, such as this one?
public class Band { public string Name { get; set; } public int YearFormed { get; set; } public int NumberOfMembers { get; set; } public int NumberOfRecords { get; set; } }
How can .NET decide on the ordering of your objects? We’ll need to give it a hint by providing an object which implements the generic IComparer of T interface like we saw in this post. We’ll let the Band objects be sorted by their names:
public class BandNameComparer : IComparer<Band> { public int Compare(Band x, Band y) { return x.Name.CompareTo(y.Name); } }
Let’s see this in action:
SortedSet<Band> bands = new SortedSet<Band>(new BandNameComparer()); bands.Add(new Band() { YearFormed = 1979, Name = "Great band", NumberOfMembers = 4, NumberOfRecords = 10 }); bands.Add(new Band() { YearFormed = 1985, Name = "Best band", NumberOfMembers = 5, NumberOfRecords = 15 }); bands.Add(new Band() { YearFormed = 1985, Name = "Well known band", NumberOfMembers = 5, NumberOfRecords = 15 }); bands.Add(new Band() { YearFormed = 1979, Name = "Famous band", NumberOfMembers = 4, NumberOfRecords = 10 }); bands.Add(new Band() { YearFormed = 1979, Name = "Great band", NumberOfMembers = 4, NumberOfRecords = 10 }); bands.Add(new Band() { YearFormed = 1985, Name = "Best band", NumberOfMembers = 5, NumberOfRecords = 15 }); bands.Add(new Band() { YearFormed = 1985, Name = "Best band", NumberOfMembers = 5, NumberOfRecords = 15 }); bands.Add(new Band() { YearFormed = 1979, Name = "Great band", NumberOfMembers = 4, NumberOfRecords = 10 }); bands.Add(new Band() { YearFormed = 1979, Name = "Famous band", NumberOfMembers = 4, NumberOfRecords = 10 }); foreach (Band band in bands) { Debug.WriteLine(band.Name); }
This will print…
Best band
Famous band
Great band
Well known band
…so not only were the items sorted by their names but the non-unique values were rejected as well. The IComparer argument also provided a way to declare equality.
View all various C# language feature related posts here.