How do I sort an array of custom classes?

后端 未结 7 1040
暖寄归人
暖寄归人 2021-02-05 15:49

I have a class with 2 strings and 1 double (amount).

class Donator

  • string name
  • string comment
  • double amount

Now I have a A

7条回答
  •  渐次进展
    2021-02-05 16:26

    You can also use delegates:

    class Program
    {
        static void Main(string[] args)
        {
            List myDonors = new List();
            // add stuff to your myDonors list...
    
            myDonors.Sort(delegate(Donor x, Donor y) { return x.amount.CompareTo(y.amount); });
        }
    }
    
    class Donor
    {
        public string name;
        public string comment;
        public double amount;
    }
    

提交回复
热议问题