-
-
Save huobazi/cee7e8b6963f698d6297 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Sort files | |
/// <code> string folder = "c:\\windows\\"; | |
/// string[] files = Directory.GetFiles(folder); | |
/// IComparer comp = new FileComparer(FileComparer.CompareBy.FileSize); | |
/// Array.Sort(files, comp); | |
/// foreach(string file in files) | |
/// { | |
/// Console.WriteLine(file); | |
/// } </code> | |
/// </summary> | |
public class FileComparer : IComparer | |
{ | |
public enum CompareBy | |
{ | |
Name /* a-z */, | |
LastWriteTime /* oldest to newest */, | |
CreationTime /* oldest to newest */, | |
LastAccessTime /* oldest to newest */, | |
FileSize /* smallest first */ | |
} | |
// default comparison | |
int _CompareBy = (int)CompareBy.Name; | |
public FileComparer() | |
{ | |
} | |
public FileComparer(CompareBy compareBy) | |
{ | |
_CompareBy = (int)compareBy; | |
} | |
int IComparer.Compare(object x, object y) | |
{ | |
int output = 0; | |
FileInfo file1 = new FileInfo(x.ToString()); | |
FileInfo file2 = new FileInfo(y.ToString()); | |
switch (_CompareBy) | |
{ | |
case (int)CompareBy.LastWriteTime: | |
output = DateTime.Compare(file1.LastWriteTime, file2.LastWriteTime); | |
break; | |
case (int)CompareBy.CreationTime: | |
output = DateTime.Compare(file1.CreationTime, file2.CreationTime); | |
break; | |
case (int)CompareBy.LastAccessTime: | |
output = DateTime.Compare(file1.LastAccessTime, file2.LastAccessTime); | |
break; | |
case (int)CompareBy.FileSize: | |
output = Convert.ToInt32(file1.Length - file2.Length); | |
break; | |
case (int)CompareBy.Name: | |
default: | |
output = (new CaseInsensitiveComparer()).Compare(file1.Name, file2.Name); | |
break; | |
} | |
return output; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment