C# 至少一个对象必须实现 IComparable

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14141891/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:49:59  来源:igfitidea点击:

At least one object must implement IComparable

c#

提问by user1930824

using System;
using System.Xml;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            SortedSet<Player> PlayerList = new SortedSet<Player>();

            while (true)
            {
                string Input;
                Console.WriteLine("What would you like to do?");
                Console.WriteLine("1. Create new player and score.");
                Console.WriteLine("2. Display Highscores.");
                Console.WriteLine("3. Write out to XML file.");
                Console.Write("Input Number: ");
                Input = Console.ReadLine();
                if (Input == "1")
                {
                    Player player = new Player();
                    string PlayerName;
                    string Score;

                    Console.WriteLine();
                    Console.WriteLine("-=CREATE NEW PLAYER=-");
                    Console.Write("Player name: ");
                    PlayerName = Console.ReadLine();
                    Console.Write("Player score: ");
                    Score = Console.ReadLine();

                    player.Name = PlayerName;
                    player.Score = Convert.ToInt32(Score);

                    //====================================
                    //ERROR OCCURS HERE
                    //====================================
                    PlayerList.Add(player);


                    Console.WriteLine("Player \"" + player.Name + "\" with the score of \"" + player.Score + "\" has been created successfully!" );
                    Console.WriteLine();
                }
                else
                {
                    Console.WriteLine("INVALID INPUT");
                }
            }
        }
    }
}

So i keep getting the "

所以我不断得到“

At least one object must implement IComparable.

至少一个对象必须实现 IComparable。

" when trying to add a second player, the first one works, but the second one doesn't. I also MUST use SortedSetbecause that is the requirement for the work, it's school work.

" 尝试添加第二个玩家时,第一个有效,但第二个无效。我也必须使用,SortedSet因为这是工作的要求,这是学校的工作。

采纳答案by Jon Skeet

Well, you're trying to use SortedSet<>... which means you care about the ordering. But by the sounds of it your Playertype doesn't implement IComparable<Player>. So what sort order would you expect to see?

好吧,您正在尝试使用SortedSet<>... 这意味着您关心顺序。但是根据它的声音,您的Player类型没有实现IComparable<Player>. 那么您希望看到什么排序顺序?

Basically, you need to tell your Playercode how to compare one player with another. Alternatively, you could implement IComparer<Player>somewhere else, and pass that comparison into the constructor of SortedSet<>to indicate what order you want the players in. For example, you could have:

基本上,您需要告诉您的Player代码如何将一名球员与另一名球员进行比较。或者,您可以在IComparer<Player>其他地方实现,并将该比较传递给 的构造函数SortedSet<>以指示您希望玩家处于什么顺序。例如,您可以:

public class PlayerNameComparer : IComparer<Player>
{
    public int Compare(Player x, Player y)
    {
        // TODO: Handle x or y being null, or them not having names
        return x.Name.CompareTo(y.Name);
    }
}

Then:

然后:

// Note name change to follow conventions, and also to remove the
// implication that it's a list when it's actually a set...
SortedSet<Player> players = new SortedSet<Player>(new PlayerNameComparer());

回答by Philip Kendall

Make your Playerclass implement IComparable.

让你的Player班级实现IComparable.

回答by WraithNath

You Player class needs to implement the IComparable interface..

您的 Player 类需要实现 IComparable 接口。

http://msdn.microsoft.com/en-gb/library/system.icomparable.aspx

http://msdn.microsoft.com/en-gb/library/system.icomparable.aspx

回答by CorrugatedAir

Your Player class must implement the IComparable interface. The SortedSet holds the items in a sorted order, but how would it know what the sorted order is if you haven't told it how to sort them (using IComparable)?

您的 Player 类必须实现 IComparable 接口。SortedSet 以排序的顺序保存项目,但是如果您没有告诉它如何对它们进行排序(使用 IComparable),它怎么知道排序的顺序是什么?

回答by user12062394

This is a more general answer to this error i suppose.

我想这是对这个错误的更一般的回答。

This line will fail with the error you got:

此行将失败并显示您收到的错误:

Items.OrderByDescending(t => t.PointXYZ);

However you can specify how to compare it directly:

但是,您可以指定如何直接比较它:

Items.OrderByDescending(t => t.PointXYZ.DistanceTo(SomeOtherPoint))

Then you dont need the IComparable interface. Depends on the API you are using. In my case i have a Point and a DistanceTo-method. (Revit API) But an integer should be even easier to determine the "size/position" of.

那么你不需要 IComparable 接口。取决于您使用的 API。就我而言,我有一个 Point 和一个 DistanceTo 方法。(Revit API) 但是整数应该更容易确定“大小/位置”。