How do I sort an array of custom classes?

后端 未结 7 1037
暖寄归人
暖寄归人 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

    Here is a sort without having to implement an Interface. This is using a Generic List

        List list = new List();
        Donator don = new Donator("first", "works", 98.0);
        list.Add(don);
        don = new Donator("first", "works", 100.0);
        list.Add(don);
        don = new Donator("middle", "Yay", 101.1);
        list.Add(don);
        don = new Donator("last", "Last one", 99.9);
        list.Add(don);
        list.Sort(delegate(Donator d1, Donator d2){ return d1.amount.CompareTo(d2.amount); });
    

提交回复
热议问题