System.IO.DirectoryInfo.GetFiles

  • Thread starter Thread starter Scott Durrett
  • Start date Start date
S

Scott Durrett

Does anyone know how to retrieve the file list sorted or how to sort this
thing? I'm wanting to sort them by date.

Thanks
Scott
 
Hello,
I don't know how to retrieve a sorted list but I can tell you how to
sort it. Write a class that implements (inherits from) IComparer. Check
that if the objects passed as the parameters are FileInfo object apply a
check on the date (Creation/Last modified/Last accessed which ever you
want) and return 1, 0 or -1 appropriately. Suppose the Compare method
signature is

protected void Compare(object x,object y)

then.. if
x < y : return -1
x = y : return 0
x > y : return 1

You can store the FileInfo array in an ArrayList and call ArrayList.Sort
method passing your IComparer's object as IComparer as parameter.

MyComparer myComp = new MyComparer;
FileInfo[] fileInfoArray = System.IO.DirectoryInfo.GetFiles("C:\Folder
1");
ArrayList list = new ArrayList(fileInfoArray);
list.Sort(myComp);

HTH. Cheers :)
Maqsood Ahmed [MCP C#,SQL Server]
Kolachi Advanced Technologies
http://www.kolachi.net
 
Hello,
I don't know how to retrieve a sorted list but I can tell you how to
sort it. Write a class that implements (inherits from) IComparer. Check
that if the objects passed as the parameters are FileInfo object apply a
check on the date (Creation/Last modified/Last accessed which ever you
want) and return 1, 0 or -1 appropriately. Suppose the Compare method
signature is

protected void Compare(object x,object y)

then.. if
x < y : return -1
x = y : return 0
x > y : return 1

You can store the FileInfo array in an ArrayList and call ArrayList.Sort
method passing your IComparer's object as IComparer as parameter.

MyComparer myComp = new MyComparer;
FileInfo[] fileInfoArray = System.IO.DirectoryInfo.GetFiles("C:\Folder
1");
ArrayList list = new ArrayList(fileInfoArray);
list.Sort(myComp);

HTH. Cheers :)
Maqsood Ahmed [MCP C#,SQL Server]
Kolachi Advanced Technologies
http://www.kolachi.net
 
Maqsood Ahmed said:
You can store the FileInfo array in an ArrayList and call ArrayList.Sort
method passing your IComparer's object as IComparer as parameter.

MyComparer myComp = new MyComparer;
FileInfo[] fileInfoArray = System.IO.DirectoryInfo.GetFiles("C:\Folder
1");
ArrayList list = new ArrayList(fileInfoArray);
list.Sort(myComp);

Or use the static Array.Sort(array, IComparer) method

Array.Sort(fileInfoArray ,new MyComp());
 
Back
Top