Sorting a List of objects in C#

后端 未结 11 1892
终归单人心
终归单人心 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:35

    The List class makes this trivial for you, since it contains a Sort method. (It uses the QuickSort algorithm, not Bubble Sort, which is typically better anyway.) Even better, it has an overload that takes a Comparison argument, which means you can pass a lambda expression and make things very simple indeed.

    Try this:

    CarList.Sort((x, y) => DateTime.Compare(x.CreationDate, y.CreationDate));
    

提交回复
热议问题