how to sort List in c# / .net

前端 未结 4 1341
时光说笑
时光说笑 2021-01-11 10:25

I have a class PropertyDetails:

public class PropertyDetails
{

     public int Sequence { get; set; }

     public int Length { get; set; }

           


        
4条回答
  •  长发绾君心
    2021-01-11 11:01

    If you want to sort the existing list in-place then you can use the Sort method:

    List propertyDetailsList = ...
    propertyDetailsList.Sort((x, y) => x.Sequence.CompareTo(y.Sequence));
    

    If you want to create a new, sorted copy of the list then you can use LINQ's OrderBy method:

    List propertyDetailsList = ...
    var sorted = propertyDetailsList.OrderBy(x => x.Sequence).ToList();
    

    (And if you don't need the results as a concrete List then you can omit the final ToList call.)

提交回复
热议问题