I have a class with 2 strings and 1 double (amount).
class Donator
Now I have a A
If you implement IComparable
public class Donator :IComparable
{
public string name { get; set; }
public string comment { get; set; }
public double amount { get; set; }
public int CompareTo(Donator other)
{
return amount.CompareTo(other.amount);
}
}
You can then call sort on whatever you want, say:
var donors = new List();
//add donors
donors.Sort();
The .Sort()
calls the CompareTo()
method you implemented for sorting.
There's also the lambda alternative without IComparable
:
var donors = new List();
//add donors
donors.Sort((a, b) => a.amount.CompareTo(b.amount));