简体   繁体   English

如何在C#中对数组排序?

[英]how do I sort an array in C#?

I have this code for displaying a list from an array: 我有这段代码用于显示数组中的列表:

<table cellpadding="0" cellspacing="5" border="0" class="listTable">
@foreach(var row in list){
    <tr>
        <td>@row[0]</td>
        <td>@row[1]</td>
        <td>@row[2]</td>
        <td>@row[3]</td>
    </tr>
}
</table>

What I want is to sort the array "list" by index 3 in an ascending order before showing the list. 我想要的是在显示列表之前按索引3的升序对数组“列表”进行排序。

I have searched for hours now, because I was sure this had been adressed before, but I found nothing. 我已经搜索了几个小时,因为我确定以前已经解决了这个问题,但是我什么都没找到。 Could be that I am using wrong terms when searching, if so then please point me in the right direction. 可能是我在搜索时使用了错误的术语,如果是的话,请指出正确的方向。

SImply use Enumerable.OrderBy<T>() 简单地使用Enumerable.OrderBy<T>()

<table cellpadding="0" cellspacing="5" border="0" class="listTable">
@foreach(var row in list.OrderBy(i=>i[3])){
    <tr>
        <td>@row[0]</td>
        <td>@row[1]</td>
        <td>@row[2]</td>
        <td>@row[3]</td>
    </tr>
}
</table>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM