.NET / C# - 将列表转换为 SortedList

发布于 2024-07-24 07:23:11 字数 122 浏览 3 评论 0原文

将列表转换为排序列表的最佳方法是什么? 有什么好的方法可以不用循环呢? 有什么聪明的方法可以用 OrderBy() 来做到这一点吗?

总结 请阅读所有答案和评论。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

凉栀 2024-07-31 07:23:11

您的意思是:

  1. 您有一个 List 并希望将其就地排序?
  2. 您有一个 List 并希望创建另一个本身已排序的“列表”
  3. 您有一个 List 并希望创建一个 SortedListT,T> 其中键与值相同

假设输入:

var x = new List<int>() { 3, 2, 1 };    

1 是微不足道的

x.Sort();

2 是微不足道的

// sx is an IOrderedEnumerable<T>, you can call ToList() on it if you want
var sx = x.OrderBy(i => i); 

3 是微不足道的副本

var s = new SortedList<int,int>(t.ToDictionary(i => i));

并且更高效:

var s = new SortedList<int,int>();
foreach (var i in x) { s[i] = [i]; }

我不明白为什么你想做 3 但就这样吧。

Do you mean:

  1. you have a List<T> and wish it to be sorted in place?
  2. you have a List<T> and wish to create another 'list' which is itself sorted
  3. you have a List<T> and wish to make a SortedList<T,T> where the key is the same as the value

Assuming input:

var x = new List<int>() { 3, 2, 1 };    

1 is trivial

x.Sort();

2 is trivial

// sx is an IOrderedEnumerable<T>, you can call ToList() on it if you want
var sx = x.OrderBy(i => i); 

3 is trivial with a copy

var s = new SortedList<int,int>(t.ToDictionary(i => i));

and more efficiently:

var s = new SortedList<int,int>();
foreach (var i in x) { s[i] = [i]; }

I can't see why you would want to do 3 but there you go.

滥情空心 2024-07-31 07:23:11
var list = new List<string>();
var sortedList = new SortedList<string, string>(list.ToDictionary(s => s));

现在我不知道这有多高效,但它只是一行代码:) 另外,在这个例子中我只是使用字符串本身作为选择器。 在实际场景中,您应该提前知道要使用什么作为选择器。

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.

著墨染雨君画夕 2024-07-31 07:23:11

了解 List 是一个智能数组,SortedList 是一个键/值二叉树。 由于它们的结构之间没有关系,因此不可能有比简单地从列表中取出每个元素并将其放入树中更有效的方法。

如果您的意思是“排序列表”而不是“SortedList”,那么通过 List.Sort() 或适当的 OrderBy() 对列表进行排序就很简单了

Understand that a List<T> is a smart array, and a SortedList<T, U> is a key/value binary tree. Since there's no relationship between their structures, there can't possibly be a more effective way to do it rather than simply taking each element from the list and putting it into the tree.

If you mean "sorted list" instead of "SortedList," then it's trivial to sort your list via either List.Sort() or an appropriate OrderBy().

苹果你个爱泡泡 2024-07-31 07:23:11
List unsortedPersons = new List();
// ... Populate unsortedPersons ...
var sorted = from person in unsortedPersons
             orderby person.Name
             select person;

我相信 LINQ 为您提供了一个 ISortedEnumerable,这可能足以满足您的目的。

List unsortedPersons = new List();
// ... Populate unsortedPersons ...
var sorted = from person in unsortedPersons
             orderby person.Name
             select person;

The LINQ gives you an ISortedEnumerable i believe, which may be good enough for your purposes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文