Jt2ouan
Jt2ouan

Reputation: 1962

c# sort a list by a column alphabetically

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.

Upvotes: 1

Views: 1755

Answers (2)

Kyle Nunery
Kyle Nunery

Reputation: 2049

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

Upvotes: 3

Nikhil Agrawal
Nikhil Agrawal

Reputation: 48600

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();

Upvotes: 5

Related Questions