Alex Rivera | Logout

Sort a Hashset .Net 3.5

Asked 2012-05-08T09:13:10.453
27

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

Edit
Report

1 Answer

18

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.

answered 2012-05-08T09:39:33.847

Your Answer