Tuesday, September 07, 2004

class ArrayListKeyDescend:IComparer
{
public int Compare(object a,object b)
{
return (-1)*Comparer.Default.Compare(((DictionaryEntry)a).Key,((DictionaryEntry)b).Key);
}
}
class ArrayListKeyAscend:IComparer
{
public int Compare(object a,object b)
{
return Comparer.Default.Compare(((DictionaryEntry)a).Key,((DictionaryEntry)b).Key);
}
}
class ArrayListValueDescend:IComparer
{
public int Compare(object a,object b)
{
return (-1)*Comparer.Default.Compare(((DictionaryEntry)a).Value,((DictionaryEntry)b).Value);
}
}
class ArrayListValueAscend:IComparer
{
public int Compare(object a,object b)
{
return Comparer.Default.Compare(((DictionaryEntry)a).Value,((DictionaryEntry)b).Value);
}
}
void Page_Load(Object sender, EventArgs e)
{

ArrayList al = new ArrayList();
DictionaryEntry de = new DictionaryEntry();

de.Key = 1;
de.Value = "AAAA";
al.Add(de);

de.Key = 4;
de.Value = "DDDD";
al.Add(de);

de.Key = 2;
de.Value = "BBBB";
al.Add(de);

de.Key = 26;
de.Value = "ZZZZ";
al.Add(de);

de.Key = 10;
de.Value = "JJJJ";
al.Add(de);

Response.Write("ArrayList in original state
");

for(int i=0;i }

Response.Write("
");

al.Sort(new ArrayListKeyDescend());
Response.Write("Sort by KEY Descending
");
for(int i=0;i }

Response.Write("
");

al.Sort(new ArrayListKeyAscend());
Response.Write("Sort by KEY Ascending
");
for(int i=0;i }

Response.Write("
");

al.Sort(new ArrayListValueDescend());
Response.Write("Sort by VALUE Descending
");
for(int i=0;i }

Response.Write("
");

al.Sort(new ArrayListValueAscend());
Response.Write("Sort by VALUE Descending
");
for(int i=0;i }

al = null;
}