Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
对C#集合进行排序_C#_Sorting_Arraylist_Collections - Fatal编程技术网

对C#集合进行排序

对C#集合进行排序,c#,sorting,arraylist,collections,C#,Sorting,Arraylist,Collections,我需要帮助排序收集对象。My Collection类提供了围绕ArrayList的功能,ArrayList保存客户对象列表。我在类中添加了一个排序方法。我正在客户类中实现IComparable,并编写了一个CompareTo方法。我的排序函数失败并抛出错误:System.ArgumentException:至少一个对象必须实现IComparable 我的目标是按姓氏、姓氏、客户编号对客户进行排序 我编写了一个小的控制台应用程序来重现这一点: using System; using System.

我需要帮助排序收集对象。My Collection类提供了围绕ArrayList的功能,ArrayList保存客户对象列表。我在类中添加了一个排序方法。我正在客户类中实现IComparable,并编写了一个CompareTo方法。我的排序函数失败并抛出错误:System.ArgumentException:至少一个对象必须实现IComparable

我的目标是按姓氏、姓氏、客户编号对客户进行排序

我编写了一个小的控制台应用程序来重现这一点:

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

namespace ConsoleApp4Testing
{
    class Program
    {
        static void Main(string[] args)
        {
            Customers customers = new Customers();
            Customer customer = new Customer("C00066", "John", "Smith", "1234 Main St", "", "Boise", "ID", "53432", "US");
            customers.Add(customer);
            customer = new Customer("C00017", "Bob", "Jones", "1001 First Ave", "", "Detroit", "MI", "84772", "US");
            customers.Add(customer);
            customer = new Customer("C00024", "Susan", "Day", "PO Box 2509", "", "Dallas", "TX", "57212", "US");
            customers.Add(customer);
            customer = new Customer("C00009", "Bill", "Mason", "987 Washington Av", "", "Los Angeles", "CA", "90254", "US");
            customers.Add(customer);
            customer = new Customer("C00042", "Alice", "Jones", "1401 G St", "", "Atlanta", "GA", "65354", "US");
            customers.Add(customer);
            customer = new Customer("C00035", "Joan", "King", "879 Chestnut St", "", "Philadelphia", "PA", "22531", "US");
            customers.Add(customer);
            customer = new Customer("C00013", "John", "Smith", "67 Filmore Ave", "", "Chicago", "IL", "61535", "US");
            customers.Add(customer);
            Console.WriteLine("Customers in order as added:");
            foreach (Customer cust in customers)
            {
                Console.WriteLine(cust.AccountNo + ", " + cust.FirstName + " " + cust.LastName);

            }
            Console.WriteLine();

            customers.Sort();
            Console.WriteLine("Customers sorted by Lastname, FirstName, AccountNo:");
            foreach (Customer cust in customers)
            {
                Console.WriteLine(cust.AccountNo + ", " + cust.FirstName + " " + cust.LastName);
            }
            Console.ReadLine();
        }
    }
}
…然后是我的客户和客户类别:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ConsoleApp4Testing
{
    public class Customers : IEnumerable
    {
        private ArrayList m_customerList;

        public Customers()
        {
            m_customerList = new ArrayList();
        }

        public Customer this[int index]
        {
            get { return (Customer)m_customerList[index]; }
            set
            {
                if (index > (m_customerList.Count - 1))
                { m_customerList.Add(value); }
                else
                { m_customerList[index] = value; }
            }
        }
        public Customer this[Guid custId]
        {
            get { return (Customer)m_customerList[indexof(custId)]; }
        }
        public int indexof(Guid custId)
        {
            int i = 0;
            foreach (Customer cst in this.m_customerList)
            {
                if (cst.ID == custId) { break; }
                i++;
            }
            if (i >= this.count)
            { return -1; }
            else
            { return i; }
        }
        public bool Exists(Guid custId)
        {
            int i = 0;
            foreach (Customer cst in this.m_customerList)
            {
                if (cst.ID == custId) { break; }
                i++;
            }
            if (i >= this.count)
            { return false; }
            else
            { return true; }
        }
        public void Add(Customer customer)
        {
            //if (this.indexof(customer.ID) < 0)  //Don't add the customer if it already exists
            //{ 
                this.m_customerList.Add(customer);
            //}
        }
        public void Sort()
        {
            try { 
                this.m_customerList.Sort(); 
            }
            catch (Exception ex)
            { System.Diagnostics.Debug.Print(ex.ToString()); }
        }
        public int count
        {
            get { return m_customerList.Count; }
        }
        // IEnumerable Interface Implementation:
        //   Declaration of the GetEnumerator() method 
        //   required by IEnumerable
        public IEnumerator GetEnumerator()
        {
            return new customerEnumerator(this);
        }

        // Inner class implements IEnumerator interface:
        private class customerEnumerator : IEnumerator
        {
            private int position = -1;
            private Customers cstmrs;

            public customerEnumerator(Customers cstmrs)
            {
                this.cstmrs = cstmrs;
            }

            // Declare the MoveNext method required by IEnumerator:
            public bool MoveNext()
            {
                if (position < cstmrs.m_customerList.Count - 1)
                {
                    position++;
                    return true;
                }
                else
                {
                    return false;
                }
            }

            // Declare the Reset method required by IEnumerator:
            public void Reset()
            {
                position = -1;
            }

            // Declare the Current property required by IEnumerator:
            public object Current
            {
                get
                {
                    return cstmrs.m_customerList[position];
                }
            }
        }
    }

    public class Customer : IComparable<Customer>
    {
        private Guid _id;
        private string _acctNo;
        private string _frstNm;
        private string _lastNm;
        private string _addr1;
        private string _addr2;
        private string _city;
        private string _st;
        private string _postal;
        private string _country;

        public Customer(Guid id)
        {
            this._id = id;
        }
        public Customer(string acctNo, string frstNm, string lastNm, string addr1, string addr2, string city, string state, string postal, string country)
        {
            this._acctNo = acctNo;
            this._frstNm = frstNm;
            this._lastNm = lastNm;
            this._addr1 = addr1;
            this._addr2 = addr2;
            this._city = city;
            this._st = state;
            this._postal = postal;
            this._country = country;
        }
        public int CompareTo(Customer c)
        {
            int compare;
            compare = String.Compare(this.LastName, c.LastName, true);
            if (compare == 0)
            {
                compare = this.FirstName.CompareTo(c.FirstName);
                if (compare == 0)
                {
                    compare = this.AccountNo.CompareTo(c.AccountNo);
                }
            }
            return compare;
        }
        public Guid ID 
        { 
            get { return _id; }
            set { this._id = value; }
        }
        public string AccountNo
        {
            get { return _acctNo; }
            set { this._acctNo = value; }
        }
        public string FirstName
        {
            get { return _frstNm; }
            set { this._frstNm = value; }
        }
        public string LastName
        {
            get { return _lastNm; }
            set { this._lastNm = value; }
        }
        public string Address1
        {
            get { return _addr1; }
            set { this._addr1 = value; }
        }
        public string Address2
        {
            get { return _addr2; }
            set { this._addr2 = value; }
        }
        public string City
        {
            get { return _city; }
            set { this._city = value; }
        }
        public string State
        {
            get { return _st; }
            set { this._st = value; }
        }
        public string Postal
        {
            get { return _postal; }
            set { this._postal = value; }
        }
        public string Country
        {
            get { return _country; }
            set { this._country = value; }
        }
    }
}
使用系统;
使用系统集合;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
命名空间控制台测试
{
公共类客户:IEnumerable
{
私人ArrayList m_customerList;
公众客户()
{
m_customerList=新的ArrayList();
}
公共客户本[int索引]
{
获取{return(Customer)m_customerList[index];}
设置
{
如果(索引>(m_customerList.Count-1))
{m_customerList.Add(value);}
其他的
{m_customerList[index]=value;}
}
}
公共客户此[Guid custId]
{
获取{return(Customer)m_customerList[indexof(custId)];}
}
public int indexof(Guid custId)
{
int i=0;
foreach(此.m_customerList中的客户cst)
{
如果(cst.ID==custId){break;}
i++;
}
如果(i>=this.count)
{return-1;}
其他的
{返回i;}
}
存在公共bool(Guid custId)
{
int i=0;
foreach(此.m_customerList中的客户cst)
{
如果(cst.ID==custId){break;}
i++;
}
如果(i>=this.count)
{返回false;}
其他的
{返回true;}
}
公共作废添加(客户)
{
//if(this.indexof(customer.ID)<0)//如果客户已经存在,请不要添加该客户
//{ 
this.m_customerList.Add(customer);
//}
}
公共无效排序()
{
试试{
这个.m_customerList.Sort();
}
捕获(例外情况除外)
{System.Diagnostics.Debug.Print(例如ToString());}
}
公共整数计数
{
获取{return m_customerList.Count;}
}
//IEnumerable接口实现:
//GetEnumerator()方法的声明
//IEnumerable所要求的
公共IEnumerator GetEnumerator()
{
返回新的CustomerNumerator(此);
}
//内部类实现IEnumerator接口:
私有类CustomerNumerator:IEnumerator
{
私有整数位置=-1;
私人客户;
公共CustomerNumerator(客户cstmrs)
{
this.cstmrs=cstmrs;
}
//声明IEnumerator所需的MoveNext方法:
公共图书馆
{
if(位置        List<Customer> customers = new List<Customer>();

        foreach(Customer cust in customers.OrderBy(c=>c.LastName).ThenBy(c=>c.FirstName).ThenBy(c=>c.CustomerNo))
        {
        }
List<Customer> m_customerList;
public class Customer : IComparable
{
    // ...

    public int CompareTo(object obj) {
        if (obj == null) return 1;

        Customer c = obj as Customer;
        if (c == null) 
           throw new ArgumentException("Object is not a Customer");

        int compare;
        compare = String.Compare(this.LastName, c.LastName, true);
        if (compare == 0)
        {
            compare = this.FirstName.CompareTo(c.FirstName);
            if (compare == 0)
            {
                compare = this.AccountNo.CompareTo(c.AccountNo);
            }
        }
        return compare;
    }
}