Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
107 views
in Technique[技术] by (71.8m points)

c# - List Sort how to sort the list

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)'

online compiler

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...