T
tshad
I have a couple of issues here stemming from the same code.
I have a class that will sort a FileInfo array by date.
public class CompareFileInfoByDate : IComparer
{
public int Compare(object x, object y)
{
FileInfo File1 = default(FileInfo);
FileInfo File2 = default(FileInfo);
File1 = (FileInfo)x;
File2 = (FileInfo)y;
return DateTime.Compare(File1.LastWriteTime,
File2.LastWriteTime);
}
}
This works fine.
I call it like:
Array.Sort(strFiles, new CompareFileInfoByDate());
Where strFiles is an array of FileInfo objects.
This will sort by date in ascending order.
I pass it the class but not the method to use. How does it know what to
use.
The reason I am asking is that I need to sort this in ascending and
descending order.
One way around that is to just create another method exactly the same but
just reverse the return statement to look like:
return DateTime.Compare(File2.LastWriteTime,
File1.LastWriteTime);
This works fine.
Can I have another method in class that named - CompareReverse?
If so, how would I tell Array.Sort to use the CompareReverse?
I could write another whole class but it seems like overkill if I can just
use the same class for both methods.
I looked at Array.Reverse, but it won't take an IComparer interface.
Thanks,
Tom
I have a class that will sort a FileInfo array by date.
public class CompareFileInfoByDate : IComparer
{
public int Compare(object x, object y)
{
FileInfo File1 = default(FileInfo);
FileInfo File2 = default(FileInfo);
File1 = (FileInfo)x;
File2 = (FileInfo)y;
return DateTime.Compare(File1.LastWriteTime,
File2.LastWriteTime);
}
}
This works fine.
I call it like:
Array.Sort(strFiles, new CompareFileInfoByDate());
Where strFiles is an array of FileInfo objects.
This will sort by date in ascending order.
I pass it the class but not the method to use. How does it know what to
use.
The reason I am asking is that I need to sort this in ascending and
descending order.
One way around that is to just create another method exactly the same but
just reverse the return statement to look like:
return DateTime.Compare(File2.LastWriteTime,
File1.LastWriteTime);
This works fine.
Can I have another method in class that named - CompareReverse?
If so, how would I tell Array.Sort to use the CompareReverse?
I could write another whole class but it seems like overkill if I can just
use the same class for both methods.
I looked at Array.Reverse, but it won't take an IComparer interface.
Thanks,
Tom