dmr*_*dmr 3 c# sorting list asp.net-3.5 hashset
我正在尝试获取一个独特的、按字母顺序排列的行业名称(字符串)列表。这是我的代码:
HashSet<string> industryHash = new HashSet<string>();
List<string> industryList = new List<string>();
List<string> orderedIndustries = new List<string>();
// add a few items to industryHash
industryList = industryHash.ToList<string>();
orderedIndustries = industryList.Sort(); //throws compilation error
Run Code Online (Sandbox Code Playgroud)
最后一行抛出编译错误:“无法将类型‘void’隐式转换为‘System.Collections.Generic.List’”
我究竟做错了什么?
List.Sort对原始列表进行排序,并且不返回新列表。因此,要么使用此方法,要么Enumerable.OrderBy + ToList:
高效的:
industryList.Sort();
Run Code Online (Sandbox Code Playgroud)
效率较低:
industryList = industryList.OrderBy(s => s).ToList();
Run Code Online (Sandbox Code Playgroud)