2013-05-08 52 views
1

我有一個類定義,我寫記錄這個類的List。在編寫錯誤報告之前排序列表時遇到問題。我試圖按錯誤報告寫入之前的'finderror'類型按字母順序對列表進行排序,以便在錯誤報告中對列表進行排序並組織更多。 這裏是類:C#排序由一列中按字母順序

public class types 
{ 
    public types() { } 
    public string person { get; set; } 
    public string state { get; set; } 
    public string phone# { get; set; } 
    public string finderror { get; set; } 

} 

如果我寫這篇文章,我得到以下錯誤:

List1 = List1.Sort(); 
    List1 is a global list I declared before my main. 

    Error-Cannot implicitly convert type 'void' to 'System.Collections.Generic.List<types>' 

我如何排序列表中的「finderror」列的字母順序。

+0

只是'List1.Sort()去;'沒有任何分配新建分配FY。此方法將對列表進行排序,並且**不會返回任何值。所以你沒有什麼可分配的。 http://msdn.microsoft.com/en-us/library/b0zbh7b6.aspx – 2013-05-08 00:36:06

+0

粘貼一個List1聲明的代碼。 – filipko 2013-05-08 00:42:11

回答

2

我想你想List1 = List1.OrderBy(x => x.finderror).ToList();

4

首先它只是

List1.Sort(); 

排序方法任何回報。它只是整理清單。

其次,如果你想根據屬性進行排序做到這一點

List<Types> sortedlist = List1.OrderBy(x => x.finderror).ToList();