对Hashset .Net 3.5进行排序
如何在c#.Net 3.5中对HashSet
进行排序?
您可以使用OrderBy
方法,IComparer(即http://msdn.microsoft.com/en-us/library/bb549422.aspx )或使用比较器内联一些lambda(我通常使用谓词进行比较)下面)。
根据链接查看:
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 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 */
阅读更多: http : //msdn.microsoft.com/en-us/library/bb534966.aspx
你没有。 根据定义, HashSet
未排序。
如果您想要一个已排序的哈希集,那么您应该使用SortedSet
。 它公开的方法本质上是HashSet
提供的方法的超集,包括对其内容进行排序的能力。
HashSet
如果你需要排序的hashset,那么你可以使用SortedDictionary类 – 只需使用一些虚拟类型(即bool )作为TValuegenerics参数。
SortedSet类在.NET 3.5中不可用。