C# linq sort - quick way of instantiating IComparer

后端 未结 5 1251
遥遥无期
遥遥无期 2021-02-01 03:42

When using linq and you have

c.Sort()

Is there any good inline way of defining a Comparison and/or IComparer class w

5条回答
  •  情话喂你
    2021-02-01 03:52

    If the objects in the List c already implement IComparable you wont need another one. But if you need custom comparison, you can implement IComparer in a nested class. You also can use a lambda expression to create a Comparison method on the fly:

    persons.Sort( (person1, person2) => person1.Age.CompareTo( person2.Age ) );

提交回复
热议问题