I am populating an array with instances of a class:
BankAccount[] a;
. . .
a = new BankAccount[]
{
new BankAccount("George Smith", 500m),
new BankAccount("Sid Zimmerman", 300m)
};
Once I populate this array, I would like to sort it by balance amounts. In order to do that, I would like to be able to check whether each element is sortable using IComparable.
I need to do this using interfaces. So far I have the following code:
public interface IComparable
{
decimal CompareTo(BankAccount obj);
}
But I’m not sure if this is the right solution. Any advice?
You should not define
IComparableyourself. It is already defined. Rather, you need to implementIComparableon yourBankAccountclass.Where you defined the
class BankAccount, make sure it implements theIComparableinterface. Then writeBankAccount.CompareToto compare the balance amounts of the two objects.Edit to show Jeffrey L Whitledge’s solution from comments: