By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,758 Members | 1,671 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,758 IT Pros & Developers. It's quick & easy.

Sorting Directories

P: n/a
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

Jan 29 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Hi,

Cable wrote:
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/lib...rray.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
--
Laurent Bugnion [MVP ASP.NET]
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
Jan 30 '07 #2

P: n/a

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

"Cable" <cb****@gmail.comha scritto nel messaggio
news:11**********************@v33g2000cwv.googlegr oups.com...
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

Jan 30 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.