csharp 对可枚举列表进行排序
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 对可枚举列表进行排序相关的知识,希望对你有一定的参考价值。
<span style="color: #008080; font-style: italic;">// Create a simple example list</span>
List<string> TestList = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> List<string><span style="color: #000000;">(</span><span style="color: #000000;">)</span>;
TestList.<span style="color: #0000FF;">Add</span><span style="color: #000000;">(</span><span style="color: #808080;">"Venezuela"</span><span style="color: #000000;">)</span>;
TestList.<span style="color: #0000FF;">Add</span><span style="color: #000000;">(</span><span style="color: #808080;">"Norway"</span><span style="color: #000000;">)</span>;
TestList.<span style="color: #0000FF;">Add</span><span style="color: #000000;">(</span><span style="color: #808080;">"Finland"</span><span style="color: #000000;">)</span>;
TestList.<span style="color: #0000FF;">Add</span><span style="color: #000000;">(</span><span style="color: #808080;">"Brazil"</span><span style="color: #000000;">)</span>;
TestList.<span style="color: #0000FF;">Add</span><span style="color: #000000;">(</span><span style="color: #808080;">"Germany"</span><span style="color: #000000;">)</span>;
TestList.<span style="color: #0000FF;">Add</span><span style="color: #000000;">(</span><span style="color: #808080;">"Australia"</span><span style="color: #000000;">)</span>;
TestList.<span style="color: #0000FF;">Add</span><span style="color: #000000;">(</span><span style="color: #808080;">"Fakeland"</span><span style="color: #000000;">)</span>;
<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;">(</span><span style="color: #FF0000;">delegate</span><span style="color: #000000;">(</span><span style="color: #FF0000;">string</span> A, <span style="color: #FF0000;">string</span> B<span style="color: #000000;">)</span> <span style="color: #000000;">{</span>
<span style="color: #0600FF;">return</span> A.<span style="color: #0000FF;">CompareTo</span><span style="color: #000000;">(</span>B<span style="color: #000000;">)</span>;
<span style="color: #000000;">}</span><span style="color: #000000;">)</span>;
<span style="color: #008080; font-style: italic;">// Print out the test list</span>
<span style="color: #0600FF;">foreach</span> <span style="color: #000000;">(</span><span style="color: #FF0000;">string</span> Country <span style="color: #0600FF;">in</span> TestList<span style="color: #000000;">)</span>
Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">(</span>Country<span style="color: #000000;">)</span>;
<span style="color: #008080; font-style: italic;">/*
Results:
Australia
Brazil
Finland
Germany
Norway
Venezuela
*/</span>
以上是关于csharp 对可枚举列表进行排序的主要内容,如果未能解决你的问题,请参考以下文章
csharp Python中内置的枚举函数可以把一个列表变成索引 - 元素对,这样就可以在对循环中同时迭代索引和元素本身: