如何实现IComparable对自定义类型的数组进行排序? [英] How do you implement IComparable to sort an array of a custom type?

查看:54
本文介绍了如何实现IComparable对自定义类型的数组进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为C#类编写了一个程序,需要弄清楚如何实现IComparable以便能够对自定义类型的数组进行排序.它编译没有错误,但是在运行时抛出异常:

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:无法比较数组中的两个元素.---> System.ArgumentException:至少一个对象必须实现IComparable.

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

我已经搜索了数小时,以找到无济于事的解决方案.关于这个主题有很多信息,但是我可能只是想得太深了.我将发布该程序,如果有人可以向我指出正确的方向并给出解释,我将永远感激不已,因为截止日期正在迅速接近.
P.S.这是我第一次在这里发布文章,因此在批评我的缺点时请保持谦虚.

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);
      } 
   } 
} 

推荐答案

异常消息非常清晰.

must implement IComparable

这意味着您必须为 Player 类实现 IComparable .

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

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

这篇关于如何实现IComparable对自定义类型的数组进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆