c# sort a list by a column alphabetically

耗尽温柔 提交于 2019-12-31 04:21:34

问题


I have a class defined and I write records to this class to a List. Having trouble sorting the list before I write an error report. I'm trying to sort the list alphabetically by the 'finderror' type before the error report is written so that the list is sorted and more organized in the error report. Here is the class:

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; }

}

If I write this, I get the following error:

    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>'

How do i sort by the 'finderror' column in the list alphabetically.


回答1:


I think you want List1 = List1.OrderBy( x => x.finderror ).ToList();




回答2:


Firstly its just

List1.Sort();

Sort method return nothing. It just sorts the list.

Secondly if you want to sort based on a property do this

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


来源:https://stackoverflow.com/questions/16430833/c-sharp-sort-a-list-by-a-column-alphabetically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!