Sort a Hashset .Net 3.5
Asked Answered
S

3

24

How can one sort a HashSet<string> in c# .Net 3.5 ?

Sensation answered 8/5, 2012 at 9:13 Comment(2)
You can't. HashSet is not sorted by definition. Do you want to create a sorted copy of it that is an array?Rhyme
Sort in-place or a sorted copy?Miramirabeau
P
36

You don't. By definition, a HashSet is not sorted.

If you want a sorted hash set, then you should use a SortedSet. The methods it exposes are essentially a superset of those provided by HashSet, including the ability to sort its contents.

Plasmasol answered 8/5, 2012 at 9:16 Comment(6)
Note that a SortedSet will not contain the distinct values you would have as when using a HashSet, but you could load Distinct values when constructing it with the appropriate extension method.Independent
@Independent I don't understand your comment.Gnatcatcher
I meant, if you need Distinct values in a SortedSet you need to call .Distinct() to get a distinct view of them. If you need a sorted HashSet you need to call .OrderBy() to get a sorted view.Independent
A HashSet doesn't have distinct values, either. So I don't really know what the point of your comment is.Plasmasol
My apologies, Set is the key word here, not hash. SortedSet is also no duplicates. "A SortedSet<T> maintains a sorted order as elements are inserted and deleted without affecting performance. Duplicate elements are not allowed."Independent
SortedSet is not available in 3.5Platitudinous
I
17

You can use the OrderBy method, either an IComparer (i.e. http://msdn.microsoft.com/en-us/library/bb549422.aspx ) or using your comparer inline with some lambdas (i usually use predicates for my comparisons as per below).

See as per link:

        class Pet
        {
            public string Name { get; set; }
            public int Age { get; set; }
        }

        public static void OrderByEx1()
        {
            Pet[] pets = { new Pet { Name="Barley", Age=8 },
                           new Pet { Name="Boots", Age=4 },
                           new Pet { Name="Whiskers", Age=1 } };

            IEnumerable<Pet> query = pets.OrderBy(pet => pet.Age);

            foreach (Pet pet in query)
            {
                Console.WriteLine("{0} - {1}", pet.Name, pet.Age);
            }
        }

        /*
         This code produces the following output:

         Whiskers - 1
         Boots - 4
         Barley - 8
        */

Read more: http://msdn.microsoft.com/en-us/library/bb534966.aspx

Independent answered 8/5, 2012 at 9:17 Comment(2)
This obviously doesn't sort the hashset itself, but returns a sorted view of the hashset.Gnatcatcher
Yes that's true, the values contained within a HashSet are not sorted but can be accessed in a sorted view (or copy).Independent
N
17

HashSet<string> is not sorted by design. If you want to sort the items once (~not often) then you can use OrderBy LINQ method (because HashSet<string> implements IEnumerable<string>): hs.OrderBy(s => s);

If you need sorted hashset then you can use SortedDictionary class - just use some dummy type (i.e. bool) for TValue generic parameter.

The SortedSet class is not available in .NET 3.5.

Norikonorina answered 8/5, 2012 at 9:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.