按ID排序数组

时间:2015-06-07 04:16:00

标签: c# arrays sorting multidimensional-array

我尝试对5个客户对象进行排序,并使用客户ID订单对其进行排序,同时仍保留客户的名称和总欠款。我还不确定如何使用ID号进行排序,同时仍然保持客户名称和与ID相关联的总数。我应该public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3){ LinearLayout tmpll = (LinearLayout) view; TextView tmptv = (TextView) tmpll.findViewById(R.id.negara_name); pos = negara.indexOf(tmptv.getText()); 吗?我已经设置并到位并且客户已经完成。

string[] arrayOfString {}

2 个答案:

答案 0 :(得分: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);
    }
}

答案 1 :(得分:-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
    }