A
anonieko
I have a directory of files and I want to sort in in Array List in
a customized way. How do I do that?class main
{
static void Main(string[] args)
{
string[] items = Directory.GetFiles( "c:\dir", "*.*");
ArrayList al = new ArrayList( items);
TransIdComparerClass myComp = new TransIdComparerClass();
al.Sort(myComp);
string topFile = (string) al[0];
}
}
/// <summary>
/// For example. This IComparer is used to sort based
// on the fourth character and onwards in each filename.
/// </summary>
public class TransIdComparerClass : IComparer
{
// Calls CaseInsensitiveComparer.Compare with the parameters
reversed.
int IComparer.Compare( Object x, Object y )
{
string a = Path.GetFileName( (string) x);
string b = Path.GetFileName( (string) y);
return ( a.Substring(3).CompareTo(b.Substring(3)) );
}
}
a customized way. How do I do that?class main
{
static void Main(string[] args)
{
string[] items = Directory.GetFiles( "c:\dir", "*.*");
ArrayList al = new ArrayList( items);
TransIdComparerClass myComp = new TransIdComparerClass();
al.Sort(myComp);
string topFile = (string) al[0];
}
}
/// <summary>
/// For example. This IComparer is used to sort based
// on the fourth character and onwards in each filename.
/// </summary>
public class TransIdComparerClass : IComparer
{
// Calls CaseInsensitiveComparer.Compare with the parameters
reversed.
int IComparer.Compare( Object x, Object y )
{
string a = Path.GetFileName( (string) x);
string b = Path.GetFileName( (string) y);
return ( a.Substring(3).CompareTo(b.Substring(3)) );
}
}