【发布时间】:2021-02-08 00:38:55
【问题描述】:
我正在尝试对我正在查看的page 列表进行排序,但我不知道为什么我的代码不起作用。我收到以下错误。
(41:21) 没有给出与所需参数相对应的参数 'List.Sort(int, int, IComparer)'
我正在尝试和那个年纪大的人做同样的事情,但我正在做排名。我不明白我哪里出错了。
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);
}
}
}
}
我也试过了
li.Sort( (pl[0],pl[1])=>pl[0].rank.CompareTo(pl[1].rank) );
但我明白了:
(40:35) 语法错误,应为“,” (40:37) 语法错误,应为“,”
如果我做
li.Sort( (pl,pl[0])=>pl.rank.CompareTo(pl.rank) );
如果我重新移动 1 [],我会得到相同的结果:
li.Sort( (pl,pl)=>pl.rank.CompareTo(pl.rank) );
我明白了:
(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'
我这里完全是瞎子我不明白这个功能的基础知识
li.Sort( (x,y)=>x.rank.CompareTo(y.rank) );
我不明白 x 或 y 是什么它不是 pl 它不是 pl[] 它是什么我不明白
好吧,只是为了好玩,我把它当作 x 和 y 而不是其他任何东西,它编译了所以它应该可以工作,我不明白为什么它编译但是好的,......现在问题是它没有排序
online compiler new codewitch 不排序但编译问题出在哪里
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);
}
}
}
}
【问题讨论】:
-
我假设您在谈论 C#,请考虑使用适当的语言和主题标记您的问题
-
嗯,我确实标记了它 c#,列出和排序