Date:

Share:

C# Tip: Use a SortedSet to avoid duplicates and sort items

Related Articles

As you probably know, you can create collections of items without duplicates using a HashSet<T> resist.

It is quite useful to remove duplicates from a list of items of the same type.

How can we ensure that we always have sorted items? The answer is simple: SortedSet<T>!

HashSet: Collection without duplicates

simple HashSet Creates a collection of unordered items without duplicates.

This example

var hashSet = new HashSet<string>();
hashSet.Add("Turin");
hashSet.Add("Naples");
hashSet.Add("Rome");
hashSet.Add("Bari");
hashSet.Add("Rome");
hashSet.Add("Turin");


var resultHashSet = string.Join(',', hashSet);
Console.WriteLine(resultHashSet);

Prints this string: Turin, Naples, Rome, Bari. The order of the inserted items is maintained.

SortedSet: Sorted collection without duplicates

To sort these items, we have two approaches.

You can simply sort the collection after you have finished adding items:

var hashSet = new HashSet<string>();
hashSet.Add("Turin");
hashSet.Add("Naples");
hashSet.Add("Rome");
hashSet.Add("Bari");
hashSet.Add("Rome");
hashSet.Add("Turin");

var items = hashSet.ToList<string>().OrderBy(s => s);


var resultHashSet = string.Join(',', items);
Console.WriteLine(resultHashSet);

Or, even better, Use the correct data structure: A SortedSet<T>

var sortedSet = new SortedSet<string>();

sortedSet.Add("Turin");
sortedSet.Add("Naples");
sortedSet.Add("Rome");
sortedSet.Add("Bari");
sortedSet.Add("Rome");
sortedSet.Add("Turin");


var resultSortedSet = string.Join(',', sortedSet);
Console.WriteLine(resultSortedSet);

Both results are printed Barry, Naples, Rome, Turin. But the second approach does not require you to sort a whole list: it is more efficient, both talking about time and memory.

Use custom sorting rules

What if we wanted to use a SortedSet With a custom object, like User?

public class User  
    public string FirstName  get; set; 
    public string LastName  get; set; 

    public User(string firstName, string lastName)
    
        FirstName = firstName;
        LastName = lastName;
    

Of course, we can do this:

var set = new SortedSet<User>();

set.Add(new User("Davide", "Bellone"));
set.Add(new User("Scott", "Hanselman"));
set.Add(new User("Safia", "Abdalla"));
set.Add(new User("David", "Fowler"));
set.Add(new User("Maria", "Naggaga"));
set.Add(new User("Davide", "Bellone"));

foreach (var user in set)

    Console.WriteLine($"user.LastName user.FirstName");

But, we get an error: Our class does not know how to compare things!

Therefore we must update ours User Class so that it implements the IComparable interface:

public class User : IComparable

    public string FirstName  get; set; 
    public string LastName  get; set; 

    public User(string firstName, string lastName)
    
        FirstName = firstName;
        LastName = lastName;
    

    public int CompareTo(object obj)
    
        var other = (User)obj;
        var lastNameComparison = LastName.CompareTo(other.LastName);

        return (lastNameComparison != 0)
            ? lastNameComparison :
            (FirstName.CompareTo(other.FirstName));
    

This way, everything works as expected:

Abdalla Safia
Bellone Davide
Fowler David
Hanselman Scott
Naggaga Maria

Notice that the second David Bellone has disappeared since he was a double.

Finishing

Choosing the right type of data is essential for building powerful and high-performance applications.

In this article, we used a SortedSet Add items to the collection and expect them to be sorted and without duplicates.

I never used it in the project. So how did I know that? I just researched the libraries I used!

Occasionally, take a few minutes to read the documentation, view the most common libraries and so on: you will find lots of things you never thought existed!

Toy with your code! Investigate it. Was curious.

And enjoy!

🐧

.

Source

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Popular Articles