Hi all ,
When i saw the question , it reminded me of a sort program which i wrote to demonstrate the use of Delegates. Without much explanation (as the usage of delegate is obvious in this example ) i am pasting the code here
<CODE>
using System;
namespace test
{
public delegate int Comparer( Object a , Object b );
class Sorter
{
public static void Sort( Object [] s,Comparer comp )
{
int nlen = s.Length;
int i=0,j=0;
for( i=0; i < nlen; ++i )
{
for( j=0; j < nlen; ++j )
{
if ( comp(s,s[j]) < 0 )
{
Object t = s[j];
s[j] = s;
s = t;
}
}
}
}
public static int StringComparer( Object a , Object b )
{
String tas =(String)a;
String tbs = (String)b;
return String.Compare(tas,tbs);
}
public static int intComparer( Object a , Object b )
{
int tas =(int)a;
int tbs = (int)b;
if ( tas < tbs )
return -1;
else if ( tas > tbs )
return 1;
else { return 0; }
}
public static void Main()
{
String [] x = { "A","Z","C"};
Comparer t = new Comparer(StringComparer);
Sort(x,t);
foreach( String a in x )
{
Console.WriteLine(a);
}
Object [] x2 = { 20,10,5,80,30};
Comparer t2 = new Comparer(intComparer);
Sort(x2,t2);
foreach( int ba in x2 )
{
Console.WriteLine(ba);
}
}
}
}
</CODE>
Copy the contents of the <CODE> </CODE> block into a file ( say sort2.cs )
Go to the visual studio command prompt
csc sort2.cs
sort2
Regards
Praseed Pai
http://praseedp.blogspot.com