I'm trying to sort the list I was looking this page but I can't figure out why my code doesn't work. I get following error.
(41:21) There is no argument given that corresponds to the required formal parameter 'comparer' of 'List.Sort(int, int, IComparer)'
I'm trying to do same as that guy with age but I'm doing on rank. I don't understand where I am going wrong.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
class PlayerA{
public PlayerA(){
playerID = sID++;
}
static int sID = 0;
public int playerID = 0;
public List<PlayerA> ignore = new List<PlayerA>();
public int rank = 0;
}
namespace Rextester{
public class Program{
public static void Main(string[] args){
List<PlayerA> li = new List<PlayerA>();
PlayerA[] pl = new PlayerA[10];
for(int i=0; i<10; i++){
pl[i] = new PlayerA();
pl[i].rank = i*2;
}
pl[0].rank = 20;
pl[1].rank = 15;
for(int i=0; i<10; i++){
li.Add(pl[i]);
}
li = li.Sort((pl[0], pl[1]), pl[0].rank.CompareTo(pl[1].rank));
for(int i=0; i<10; i++){
Console.WriteLine(pl[i].rank);
}
}
}
}
I tried also
li.Sort( (pl[0],pl[1])=>pl[0].rank.CompareTo(pl[1].rank) );
but I get:
(40:35) Syntax error, ',' expected (40:37) Syntax error, ',' expected
if I make
li.Sort( (pl,pl[0])=>pl.rank.CompareTo(pl.rank) );
I get same if I reamove 1 [] like:
li.Sort( (pl,pl)=>pl.rank.CompareTo(pl.rank) );
I get:
(42:23) A local or parameter named 'pl' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter
(42:26) The parameter name 'pl' is a duplicate
(42:31) Ambiguity between 'PlayerA' and 'PlayerA'
(42:49) Ambiguity between 'PlayerA' and 'PlayerA'
I'm totally blind here I don't understand the basics of this function
li.Sort( (x,y)=>x.rank.CompareTo(y.rank) );
I don't understand what x or y is it's not pl it's not pl[] what is it I don't understand
ok just for fun I get it as x and y not anything else and it compiled so it should work and I don't understand why it compiled but ok, ... now prblem is it didn't sort it
online compiler new code witch doesn't sort but compiles where is the problem
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
class PlayerA{
public PlayerA(){
playerID = sID++;
}
static int sID = 0;
public int playerID = 0;
public List<PlayerA> ignore = new List<PlayerA>();
public int rank = 0;
}
namespace Rextester{
public class Program{
public static void Main(string[] args){
List<PlayerA> li = new List<PlayerA>();
PlayerA[] pl = new PlayerA[10];
for(int i=0; i<10; i++){
pl[i] = new PlayerA();
pl[i].rank = i*2;
}
pl[0].rank = 20;
pl[1].rank = 15;
for(int i=0; i<10; i++){
li.Add(pl[i]);
}
li.Sort( (x,y)=>x.rank.CompareTo(y.rank) );
for(int i=0; i<10; i++){
Console.WriteLine(pl[i].rank);
}
}
}
}
question from:https://stackoverflow.com/questions/66050128/list-sort-how-to-sort-the-list