【问题标题】:Sorting array by id按 id 排序数组
【发布时间】:2015-08-21 18:56:07
【问题描述】:

我正在尝试对 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());
    }

【问题讨论】:

  • 请出示您的代码。
  • 你为什么不把你的代码放在问题中?
  • 为什么要让我们猜测您的代码是什么样的?我们可能会猜错并给您错误的答案。那会浪费我们的时间,让我们少考虑你。
  • 你真的不希望我们少考虑你。
  • 对不起,我是新手

标签: c# arrays sorting multidimensional-array


【解决方案1】:

试试这个:

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

【讨论】:

  • 这种设置更好更干净。我想这就是我学习的方式。谢谢
  • @JOGO 是的,C# 可以自动实现属性集,您不必像在 Java 中那样编写所有的 getter 和 setter。最上面一行使用LINQ,它非常适合处理集合。
【解决方案2】:

为了对对象列表(在您的情况下为客户)进行排序,您必须在您的类中实现 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
    }

【讨论】:

  • 请永远永远不要实现使用来自读/写属性的值的GetHashCode().GetHashCode()必须在对象的任何依赖哈希码的数据结构中使用时必须保持不变——例如Dictionary&lt;,&gt;HastSet&lt;&gt;HashTable。跨度>
猜你喜欢
  • 2017-05-15
  • 1970-01-01
  • 2015-01-07
  • 2018-05-04
  • 2016-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多