Sorting a List of objects in C#

后端 未结 11 1914
终归单人心
终归单人心 2020-12-07 18:01
public class CarSpecs
{
  public String CarName { get; set; }

  public String CarMaker { get; set; }

  public DateTime CreationDate { get; set; }
}
11条回答
  •  甜味超标
    2020-12-07 18:30

    You could use LINQ:

    listOfCars.OrderBy(x => x.CreationDate);
    

    EDIT: With this approach, its easy to add on more sort columns:

    listOfCars.OrderBy(x => x.CreationDate).ThenBy(x => x.Make).ThenBy(x => x.Whatever);
    

提交回复
热议问题