When using linq and you have
c.Sort()
Is there any good inline way of defining a Comparison
and/or IComparer
class w
I've no idea what c.Sort()
is in your example, as it can be many things (do you mean List
?), but one thing that it sure isn't is LINQ. LINQ doesn't have Sort()
- it has OrderBy()
.
That said, the latter also works with IComparer
, and there's no way to create an instance of anonymous class implementing the interface "inline", so you'll have to define a class.
For List
, there is an overload which takes Comparison
. Since it's a delegate type, you can use a lambda to provide the function inline:
List xs = ...;
xs.Sort((x, y) => y - x); // reverse sort