【问题标题】:Sort a ArrayList of Objects based on parameter with IComparable使用 IComparable 根据参数对对象的 ArrayList 进行排序
【发布时间】:2021-02-07 05:03:15
【问题描述】:

我创建了一个小测试程序来获得一些使用 C# 中的 IComparable 接口的经验。我以后需要将接口用于其他东西。

在程序中,我想根据薪水对客户的 ArraList 进行排序。执行我的代码时,我总是收到“System.InvalidOperationException”异常。

我已经尝试了不同版本的代码并查看了一些教程,结果总是相同。

我的代码:

using System;
using System.Collections;

namespace CsTest
{
    public class Program
    {
        public static void Main()
        {
            Customer customer1 = new Customer()
            {
                ID = 101,
                Name = "Ted",
                Salary = 4000
            };

            Customer customer2 = new Customer()
            {
                ID = 102,
                Name = "Tod",
                Salary = 7000
            };

            Customer customer3 = new Customer()
            {
                ID = 103,
                Name = "Tom",
                Salary = 5500
            };

            ArrayList listCustomers = new ArrayList();
            listCustomers.Add(customer1);
            listCustomers.Add(customer2);
            listCustomers.Add(customer3);

            Console.WriteLine("Customers before sorting");
            foreach (Customer customer in listCustomers)
            {
                Console.WriteLine(customer.Name + " - " + customer.Salary);
            }

            // Sort() method should sort customers by salary
            listCustomers.Sort();

            Console.WriteLine("Customers after sorting");
            foreach (Customer customer in listCustomers)
            {
                Console.WriteLine(customer.Name + " - " + customer.Salary);
            }
        }
    }

public class Customer : IComparable<Customer>
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public int Salary { get; set; }

        public int CompareTo(Customer obj)
        { 
            return this.Salary.CompareTo(obj.Salary);
        }
    }
}

【问题讨论】:

  • 你从哪里得到这个异常,为什么你使用 ArrayList 而不是 List ?

标签: c# sorting arraylist icomparable


【解决方案1】:

您是否有特定原因要使用ArrayList 而不是List&lt;Customer&gt;?一般建议使用类型化列表,因为它只接受指定类型的对象。

这里的问题是ArrayList.Sort() 不使用IComparable 接口。检查注释here,其中声明您必须实现IComparer 接口并使用.Sort(comparer) 重载。

这是您的唯一选择。另一种选择(容易得多)是使用List&lt;Customer&gt;,它将使用您的IComparable 实现开箱即用。你的选择:)

【讨论】:

  • 其实我只用了ArrayList,因为教程里也用到了。我尝试使用 List 并且每个都运行良好。非常感谢!
  • 是的 ArrayList 现在应该避免使用。它的大表亲List&lt;T&gt; 做同样的事情,而且更加通用和安全。
【解决方案2】:

这是使用原始 ArrayList 的解决方案

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

namespace ConsoleApplication1
{
    class Program
    {
        public static void Main()
        {
            Customer customer1 = new Customer()
            {
                ID = 101,
                Name = "Ted",
                Salary = 4000
            };

            Customer customer2 = new Customer()
            {
                ID = 102,
                Name = "Tod",
                Salary = 7000
            };

            Customer customer3 = new Customer()
            {
                ID = 103,
                Name = "Tom",
                Salary = 5500
            };

            ArrayList listCustomers = new ArrayList();
            listCustomers.Add(customer1);
            listCustomers.Add(customer2);
            listCustomers.Add(customer3);

            Console.WriteLine("Customers before sorting");
            foreach (Customer customer in listCustomers)
            {
                Console.WriteLine(customer.Name + " - " + customer.Salary);
            }

            // Sort() method should sort customers by salary

            Customer sortCustomer = new Customer();
            listCustomers.Sort(sortCustomer);

            Console.WriteLine("Customers after sorting");
            foreach (Customer customer in listCustomers)
            {
                Console.WriteLine(customer.Name + " - " + customer.Salary);
            }
        }
    }

    public class Customer : IComparer 
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public int Salary { get; set; }

          int IComparer.Compare( Object x, Object y )  {
          return( (new CaseInsensitiveComparer()).Compare( ((Customer)x).Salary, ((Customer)y).Salary) );
        }
    }
}

【讨论】:

  • 所以我需要使用 Linq 对 ArrayList 进行排序?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-10-08
  • 1970-01-01
  • 2012-02-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-06
相关资源
最近更新 更多