按id排序数组

本文关键字:数组 排序 id | 更新日期: 2023-09-27 17:53:50

我试图排序使5个客户对象和排序与客户ID顺序,同时仍然保持客户的名称和总欠款。我不确定如何使用ID号进行排序,同时仍然保持与ID相关的客户名称和总数。我应该做一个string[] arrayOfString {}吗?我有布景和道具,客户也做好了。

public class Customer
{
    //Attributes
    private string firstName;
    private string lastName;
    private int idNumber;
    private double total;
    //Methods
    public Customer() { }
    public Customer(string aFirstName, string aLastName, int aIDNumber, double aTotal)
    {
        this.SetFirstName(aFirstName);
        this.SetLastName(aLastName);
        this.SetIDNumber(aIDNumber);
        this.SetTotal(aTotal);
        Console.WriteLine(this.Display());
    }

    //Set
    public void SetFirstName(string aFirstName)
    {
        this.firstName = aFirstName;
    }
    public void SetLastName(string aLastName)
    {
        this.lastName = aLastName;
    }
    public void SetIDNumber(int aIDNumber)
    {
        this.idNumber = aIDNumber;
    }
    public void SetTotal(double aTotal)
    {
        this.total = aTotal;
    }
    //Get
    public string GetFirstName()
    {
        return this.firstName;
    }
    public string GetLastName()
    {
        return this.lastName;
    }
    public int GetIDNumber()
    {
        return this.idNumber;
    }
    public double GetTotal()
    {
        return this.total;
    }
    public string Display()
    {
        return string.Format("Customer Created'r'n" +
        "'tName: {0} {1}'r'n" +
        "'tID: {2}'r'n" +
        "'tBalence: {3}'r'n", this.GetFirstName(), this.GetLastName(), this.GetIDNumber(), this.GetTotal());
    }

按id排序数组

试试这个:

List<Customer> sortedCustomers = customers.OrderBy(c => c.GetIDNumber()).ToList();

我也很好奇为什么你不这样实现你的类:

public class Customer
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int IDNumber { get; set; }
    public double Total { get; set; }
    public Customer() { }
    public Customer(string aFirstName, string aLastName, int aIDNumber, double aTotal)
    {
        this.FirstName = aFirstName;
        this.LastName = aLastName;
        this.IDNumber = aIDNumber;
        this.Total = aTotal;
        Console.WriteLine(this.Display());
    }
    public string Display()
    {
        return String.Format(
            "Customer Created'r'n'tName: {0} {1}'r'n'tID: {2}'r'n'tBalance: {3}'r'n",
            this.FirstName, this.LastName, this.IDNumber, this.Total);
    }
}

为了对对象列表(在您的示例中是客户)进行排序,您必须在类中实现IComparable。

一个示例类:

    public class Person : IComparable<Person>, IComparable, ICloneable
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string LastName { get; set; }
        public DateTime BirthDate { get; set; }
        public double Weight { get; set; }
        public Address Address { get; set; }
        public int Age
        {
            get
            {
                TimeSpan ts = DateTime.Now.Subtract( BirthDate );
                return new DateTime( ts.Ticks ).Year - 1;
            }
        }
        public string FullName
        {
            get
            {
                return string.Format( "{0} {1}", LastName, Name ).Trim();
            }
        }

        public override string ToString()
        {
            return string.Format( "[{0}] - {1}, born at {2:dd/MM/yyyy} ({3} years old), {4:#,##0.00} kg", ID, FullName, BirthDate, Age, Weight );
        }
        public override bool Equals( object obj )
        {
            if ( ( obj == null ) || ( !( obj is Person ) ) )
                return false;
            Person other = (Person)obj;
            return ( this.ID == other.ID ) &&
                ( this.Name == other.Name ) &&
                ( this.LastName == other.LastName ) &&
                ( this.BirthDate == other.BirthDate ) &&
                ( this.Weight == other.Weight );
        }
        public override int GetHashCode()
        {
            return ID.GetHashCode() * 2 +
                Name.GetHashCode() * 3 +
                LastName.GetHashCode() * 4;
        }

        #region IComparable<Person> Members
        public int CompareTo( Person other )
        {
            if ( other == null )
                return -1;
            return this.FullName.CompareTo( other.FullName );
        }
        #endregion
        #region IComparable Members
        public virtual int CompareTo( object obj )
        {
            if ( ( obj == null ) || ( !( obj is Person ) ) )
                return -1;
            Person other = (Person)obj;
            return CompareTo( other );
        }
        #endregion
        #region ICloneable Members
        public virtual object Clone()
        {
            return new Person() { ID = this.ID, Name = this.Name, LastName = this.LastName, BirthDate = this.BirthDate, Weight = this.Weight };
        }
        #endregion
    }