.NET / C# - 转换列表到排序列表 [英] .NET / C# - Convert List to a SortedList

查看:146
本文介绍了.NET / C# - 转换列表到排序列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是一个列表转换为排序列表的最好方法?任何好的办法做到这一点,而不通过它循环?任何聪明的方式做到这一点的排序依据()?



包裹
请仔细阅读所有的答案和注释。


解决方案

  VAR名单=新名单,LT;串>(); 
变种排序列表=新SortedList的<字符串,字符串>(list.ToDictionary(S = GT S));

现在我不知道如何有效的,这是,但它的一行代码:)另外,这个例子我只是用字符串本身的选择。在真实的情景,你应该提前知道时间,你想作为一个选择使用什么。


What is the best way to convert a List to SortedList? Any good way to do it without cycling through it? Any clever way to do it with an OrderBy()?

WRAP UP Please read all answers and comments.

解决方案

var list = new List<string>();
var sortedList = new SortedList<string, string>(list.ToDictionary(s => s));

Now I have no clue how efficient this is, but it's one line of code :) Also, in this example I just used the string itself as the selector. In a real scenario, you should know ahead of time what you'd like to use as a selector.

这篇关于.NET / C# - 转换列表到排序列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆