使用 IComparable 根据参数对对象的 ArrayList 进行排序

Sort a ArrayList of Objects based on parameter with IComparable

我创建了一个小测试程序来获得一些使用 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<Customer> 有什么具体原因吗?一般建议使用类型列表,因为它只接受指定类型的对象。

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

这是你的一个选择。另一种选择(容易得多)是使用 List<Customer> ,它将开箱即用地使用您的 IComparable 实现。您的选择:)

这是使用原始 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) );
        }
    }
}