Hi,
I have SortedList (Records1). I that I am storing data in the format of
1. Records1.Add(Item.Date.ToShortDateString() + Item.ChanceryCode + Item.Reference.ToString() + Item.Year.ToString(), Item);
2. The keys for Record1 are
1/1/2006I11833802002
1/1/2007C12473522004
1/1/2007I12473342002
1/1/2007I12473352002
1/1/2007I12473362002
1/1/2007I12473382002
1/1/2007I12473442004
1/1/2007I12473452004
1/1/2007I12473532004
1/10/2006C11867322001
1/10/2006C11869032003
My question is
I have to iterate sortedlist year wise to get values and then I will do calculations Item taxes
for Example :
2001 has
1/10/2006C11867322001
2002 has
1/1/2007I12473342002
1/1/2007I12473342002
1/1/2007I12473352002
1/1/2007I12473362002
1/1/2007I12473382002
2003
1/10/2006C11869032003
2004
1/1/2007C12473522004
1/1/2007I12473442004
1/1/2007I12473452004
1/1/2007I12473532004
Finally I print
2001 1000.00 ( It's Total += Item.Tax for this 2001 yr)
2002 2300.00 ( It's Total += Item.Tax for this 2001 yr)
...
....
Please provide help on this.
Thanks in advance
AlanPosted Jan 5, 2008, 7:51 AM
One thing you could do is to create a second SortedList. The (unique) key for this list would be the key for the first list prepended by the year and the value would be the same as the first list. This second list would then sort by year and then in the same order as before.
The code would look something like this:
using System;
using System.Collections;
class Program
{
static void Main()
{
SortedList sl = new SortedList();
// arbitrary values used
sl.Add("1/1/2006I11833802002",10);
sl.Add("1/1/2007C12473522004",20);
sl.Add("1/1/2007I12473342002",30);
sl.Add("1/1/2007I12473352002",40);
sl.Add("1/1/2007I12473362002",50);
sl.Add("1/1/2007I12473382002",60);
sl.Add("1/1/2007I12473442004",70);
sl.Add("1/1/2007I12473452004",80);
sl.Add("1/1/2007I12473532004",90);
sl.Add("1/10/2006C11867322001",100);
sl.Add("1/10/2006C11869032003",110);
SortedList sl2 = new SortedList();
string key = null;
foreach(string s in sl.Keys)
{
key = s.Substring(s.Length - 4) + s;
sl2.Add(key, sl[s]);
}
int year1 = int.Parse(sl2.GetKey(0).ToString().Substring(0,4));
int year2 = 0;
double total = double.Parse(sl2.GetByIndex(0).ToString());
double taxRate = 0.1; // or whatever
for(int i = 1; i < sl2.Keys.Count; i++)
{
year2 = int.Parse(sl2.GetKey(i).ToString().Substring(0,4));
if (year2 == year1)
{
total += double.Parse(sl2.GetByIndex(i).ToString());
if (i == sl2.Keys.Count - 1)
{
total *= (1 + taxRate);
Console.WriteLine("{0} {1:N2} It's Total += Item.Tax for this {0} yr", year1, total);
}
}
else
{
total *= (1 + taxRate);
Console.WriteLine("{0} {1:N2} It's Total += Item.Tax for this {0} yr", year1, total);
year1 = year2;
total = double.Parse(sl2.GetByIndex(i).ToString());
}
}
Console.ReadLine();
}
}