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
693 views
in Technique[技术] by (71.8m points)

c# - How do you implement IComparable to sort an array of a custom type?

I wrote a program for my C# class and need to figure out how to implement IComparable to be able to sort an array of a custom type. It compiles with no errors, but throws an exception when run:

System.InvalidOperationException: Failed to compare two elements in the array. ---> System.ArgumentException: At least one object must implement IComparable.

I've searched for hours to find a solution to no avail. There's tons of info on the subject, but I'm probably just overthinking it. I'll post the program and if someone could point me in the right direction with an explanation I'd be eternally grateful as the deadline for this is rapidly approaching.
P.S. This is my first time posting here so please be gentle when criticizing my shortcomings.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HighScores4
{
   class Program
   {
      static void Main(string[] args)
      {         
         string playerInitials;
         int playerScore;

         const int NUM_PLAYERS = 10;

         Player[] stats = new Player[NUM_PLAYERS];         

         for (int index = 0; index < NUM_PLAYERS; index++)
         {
            Console.WriteLine("Please enter your initials: ");
            playerInitials = Convert.ToString(Console.ReadLine());

            Console.WriteLine("Please enter your score: ");
            playerScore = Convert.ToInt32(Console.ReadLine());

            stats[index] = new Player(playerScore, playerInitials);            
         }

         Array.Sort(stats);   **// Exception thrown here**
         Array.Reverse(stats);

         for (int index = 0; index < NUM_PLAYERS; index++)
         {
            Console.WriteLine(stats[index].ToString());
         }

#if DEBUG
         Console.ReadKey();
#endif 

      } 
   }

   public class Player 
   {      
      public string Initials { get; set; }
      public int Score { get; set; }

      public Player(int score, string initials)
      {
         Initials = initials;
         Score = Score;
      } 

      public override string ToString()
      {
         return string.Format("{0, 3}, {1, 7}", Score, Initials);
      } 
   } 
} 
See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

The exception message is perfectly clear.

must implement IComparable

It means you have to implement IComparable for your Player class.

public class Player : IComparable<Player>
{
    ...
}

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

...