EDIT: I missed a crucial point: .NET 2.0
Consider the case where I have a list of unsorted items, for the sake of simplicity of a type like this:
class TestClass
{
DateTime SomeTime;
decimal SomePrice;
// constructor
}
I need to create a report-like output, where the total prices for each day are accumulated. There should be one line for each item, folled by the appropriate summary lines.
Take this test data:
List<TestClass> testList = new List<TestClass> {
new TestClass(new DateTime(2008,01,01), 12),
new TestClass(new DateTime(2007,01,01), 20),
new TestClass(new DateTime(2008,01,01), 18)
};
The desired output would be something like this:
2007-01-01:
20
Total: 20
2008-01-01:
12
18
Total: 30
What's the best way to approach such scenarios? In the case of such a list, I would implement the IComparable interface for TestClass, so that the list can be sorted.
To create the report itself, something like this could be used (let's assume that we have methods for tasks like accumulating the prices, keeping track of the current date etc):
for (int i=0;i<testList.Count;i++)
{
if (IsNewDate(testList[i]))
{
CreateSummaryLine();
ResetValuesForNewDate();
}
AddValues(testList[i]);
}
// a final summary line is needed to include the data for the last couple of items.
CreateSummaryLine();
This works alright, but I have a strange feeling as far as the second "CreateSummaryLines" is concerned.
In what ways do you handle such situations (especially considering the fact, the we need to work with a List<> of items rather than a pre-categorized Dictionary or something like that)?