Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

At least one object must implement IComparable

Tags:

c#

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.

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

like image 254
user1930824 Avatar asked Jan 03 '13 15:01

user1930824


People also ask

How does IComparable work C#?

C# IComparable interfaceThe IComparable interface defines a generalized type-specific comparison method that a value type or class implements to order or sort its instances. The IComparable is implemented by types whose values can be ordered or sorted. The interface requires the CompareTo method to be implemented.

Does string implement IComparable?

All numeric types (such as Int32 and Double) implement IComparable, as do String, Char, and DateTime.


2 Answers

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

Basically, you need to tell your Player code 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:

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()); 
like image 163
Jon Skeet Avatar answered Sep 20 '22 04:09

Jon Skeet


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.

like image 40
user12062394 Avatar answered Sep 23 '22 04:09

user12062394