csharp 对可枚举列表进行排序

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 对可枚举列表进行排序相关的知识,希望对你有一定的参考价值。

<span style="color: #008080; font-style: italic;">// Create a simple example list</span>
List&lt;string&gt; TestList = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> List&lt;string&gt;<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
TestList.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Venezuela&quot;</span><span style="color: #000000;">&#41;</span>;
TestList.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Norway&quot;</span><span style="color: #000000;">&#41;</span>;
TestList.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Finland&quot;</span><span style="color: #000000;">&#41;</span>;
TestList.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Brazil&quot;</span><span style="color: #000000;">&#41;</span>;
TestList.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Germany&quot;</span><span style="color: #000000;">&#41;</span>;
TestList.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Australia&quot;</span><span style="color: #000000;">&#41;</span>;
TestList.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Fakeland&quot;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
&nbsp;
<span style="color: #008080; font-style: italic;">// Sort the list by A-Z</span>
TestList.<span style="color: #0000FF;">Sort</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">delegate</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> A, <span style="color: #FF0000;">string</span> B<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> 
    <span style="color: #0600FF;">return</span> A.<span style="color: #0000FF;">CompareTo</span><span style="color: #000000;">&#40;</span>B<span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
<span style="color: #008080; font-style: italic;">// Print out the test list</span>
<span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> Country <span style="color: #0600FF;">in</span> TestList<span style="color: #000000;">&#41;</span>
    Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span>Country<span style="color: #000000;">&#41;</span>;
&nbsp;
<span style="color: #008080; font-style: italic;">/*
Results:
&nbsp;
Australia
Brazil
Finland
Germany
Norway
Venezuela
*/</span>

以上是关于csharp 对可枚举列表进行排序的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Dart 中按“枚举”对列表进行排序?

csharp 枚举到列表

Python内置函数——排序与逆序

csharp Python中内置的枚举函数可以把一个列表变成索引 - 元素对,这样就可以在对循环中同时迭代索引和元素本身:

csharp C#使用Lambda表达式为动态类型和动态属性排序通用列表

对 Flutter 应用程序的 getX dart 包中的 RxList 进行排序