Try using the same expression as the linked answer:
li.Sort( (x,y)=>x.rank.CompareTo(y.rank) );
The code you posted:
li = li.Sort((pl[0], pl[1]), pl[0].rank.CompareTo(pl[1].rank));
is completely different from the linked answer. The linked answer uses a function (delegate) in lambda form that compares two parameters named x
and y
:
(x, y) => x.Age.CompareTo(y.Age)
List.Sort doesn't return anything either, as it sorts the list in-place.
What you posted tries to pass a (PlayerA,PlayerS)
tuple and a int
to Sort
. If you write each parameter on a separate line, what you posted is :
li = li.Sort(
(pl[0], pl[1]),
pl[0].rank.CompareTo(pl[1].rank)
);
(pl[0], pl[1])
is a tuple containing the first two elements of the list. pl[0].rank.CompareTo(pl[1].rank
returns the result of comparing two specific list items. No Sort
overload accepts such parameters. The error tells you that the compiler couldn't find any matching function, and the closest match is lacking some parameters
Update
The following code :
var list=Enumerable.Range(0,10).Select(i=>new PlayerA{Rank=i*2}).ToList();
list[0].Rank=20;
list[1].Rank=15;
Console.WriteLine(String.Join(",",list.Select(x=>x.Rank)));
list.Sort((x,y)=>x.Rank.CompareTo(y.Rank));
Console.WriteLine(String.Join(",",list.Select(x=>x.Rank)));
Prints :
20,15,4,6,8,10,12,14,16,18
4,6,8,10,12,14,15,16,18,20
This dotNetFiddle snippet can be used to run the code and reproduce the results