I have some code that takes a list made up of custom class objects (called Payments) and sorts them by date, as below:
payments.Sort(delegate(Payments p1, Payments p2) { return p1.GetDate().CompareTo(p2.GetDate()); });
the GetDate() method and the payment class is below:
public class Payments
{
public string Date { get; set; }
public string Payment { get; set; }
public string Reference { get; set; }
public decimal Amount { get; set; }
public DateTime GetDate()
{
return DateTime.Parse(this.Date);
}
}
Once the list is sorted I manually go through each one and compare the date on it to the date on the next one. If they are different then nothing happens, if they are the same then I merge the data in the Payments together into a single new Payment. I then remove the two payments that were being compared and then add in the new one, the list is then resorted and continued until the list is unique by date.
Up until very recently this has been working fine, with no issues. However from today there have been multiple cases of it erroring with the message "Failed to compare two elements in the array".
I have looked around for this but I dont feel that i know enough about what could be causing it to comfortably make changes to my code. Can someone help me understand what would be causing this issue and the best way to fix it?
Thanks