Sorting Directories

  • Thread starter Thread starter Cable
  • Start date Start date
C

Cable

Hi,

I've got a simple:
DirectoryInfo[] dis = d.GetDirectories();
foreach (DirectoryInfo di in dis)

The trouble is I can't find a way to sort or order the directoryinfo[]
array, I've found a way of doing it for files, but for some reason you
can't do the same to directoryinfo.

could some one give me some pointers.

thx

Chris
 
Hi,
Hi,

I've got a simple:
DirectoryInfo[] dis = d.GetDirectories();
foreach (DirectoryInfo di in dis)

The trouble is I can't find a way to sort or order the directoryinfo[]
array, I've found a way of doing it for files, but for some reason you
can't do the same to directoryinfo.

could some one give me some pointers.

thx

Chris

Any array can be sorted using the Array.Sort method.
http://msdn2.microsoft.com/en-us/library/system.array.sort.aspx

For example, you can use the Sort( Array, IComparer ) override.
http://msdn2.microsoft.com/en-us/library/aw9s5t8f.aspx

This lets you specify a class implementing the IComparer interface, in
which any criterium can be used for the sorting algorithm.

In .NET 2.0, a nice alternative is the generic version:
http://msdn2.microsoft.com/en-us/library/cxt053xf.aspx

In this override, the comparison algorithm is not defined in a class,
but in a method which is passed as a delegate to the Sort method.

HTH,
Laurent
 
Array.Sort<DirectoryInfo>(dis, new
Comparison<DirectoryInfo>(delegate(DirectoryInfo d1, DirectoryInfo d2) {
return string.Compare(d1.Name, d2.Name); }));
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top