Sorting Files in .NET
-
I have written a small application in Managed C++ that traverses the filesystem and comes up with an array of FileInfo objects representing each file within the path provided and its subdirectories. The point of this is to sort the entire list (potentially thousands of files) by size, filename and subsets, dates, etc. regardless of the directory they reside in. The problem I am having is that I can't sort the FileInfo type. using
ArrayList* files
seems to be my best option for collecting the list, thenfiles->Add(someFileInfoObject)
to populate the array. This all works well and I get an accurate list of files. Butfiles->Sort()
needs to access IComparable::CompareTo, which is not implemented by the FileInfo class. I tried to create a new class, MyFileInfo based on FileInfo to implement IComparable, but was then told by Visual Studio that FileInfo is sealed, so I can't inherit from it. I could spend my time creating custom sorting routines, but there's got to be a way to sort FileInfo objects that I'm overlooking. If I can't inherit from FileInfo, therefore can't add the IComparable interface to it, which seems the most elegant solution, I'm planning to create a__gc class MyFileInfo : public IComparable
that contains its own FileInfo class rather than inheriting from it, then just passes along the information needed from within FileInfo. This seems it may work, but would require typing dozens of little stub routines just to allow MyFileInfo to pretend to be an extended FileInfo. The reason I'm asking instead of just doing is that sorting files seems to be something that ought to be supported by default someplace. Any suggestions or ideas? -mike -www.channelmike.com -- modified at 13:40 Friday 6th January, 2006 -
I have written a small application in Managed C++ that traverses the filesystem and comes up with an array of FileInfo objects representing each file within the path provided and its subdirectories. The point of this is to sort the entire list (potentially thousands of files) by size, filename and subsets, dates, etc. regardless of the directory they reside in. The problem I am having is that I can't sort the FileInfo type. using
ArrayList* files
seems to be my best option for collecting the list, thenfiles->Add(someFileInfoObject)
to populate the array. This all works well and I get an accurate list of files. Butfiles->Sort()
needs to access IComparable::CompareTo, which is not implemented by the FileInfo class. I tried to create a new class, MyFileInfo based on FileInfo to implement IComparable, but was then told by Visual Studio that FileInfo is sealed, so I can't inherit from it. I could spend my time creating custom sorting routines, but there's got to be a way to sort FileInfo objects that I'm overlooking. If I can't inherit from FileInfo, therefore can't add the IComparable interface to it, which seems the most elegant solution, I'm planning to create a__gc class MyFileInfo : public IComparable
that contains its own FileInfo class rather than inheriting from it, then just passes along the information needed from within FileInfo. This seems it may work, but would require typing dozens of little stub routines just to allow MyFileInfo to pretend to be an extended FileInfo. The reason I'm asking instead of just doing is that sorting files seems to be something that ought to be supported by default someplace. Any suggestions or ideas? -mike -www.channelmike.com -- modified at 13:40 Friday 6th January, 2006Sort is not something the FileInfo class really should support anyway. How about using FileInfo to gather all the particulars for each file, like filename, parent folder, size, ..., and dumping that information into a ListView, DataTable, or even an Access database. You could then provide, or use as the case may be, the sorting methods without adding the overhead of the rest of the FileInfo class. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
-
I have written a small application in Managed C++ that traverses the filesystem and comes up with an array of FileInfo objects representing each file within the path provided and its subdirectories. The point of this is to sort the entire list (potentially thousands of files) by size, filename and subsets, dates, etc. regardless of the directory they reside in. The problem I am having is that I can't sort the FileInfo type. using
ArrayList* files
seems to be my best option for collecting the list, thenfiles->Add(someFileInfoObject)
to populate the array. This all works well and I get an accurate list of files. Butfiles->Sort()
needs to access IComparable::CompareTo, which is not implemented by the FileInfo class. I tried to create a new class, MyFileInfo based on FileInfo to implement IComparable, but was then told by Visual Studio that FileInfo is sealed, so I can't inherit from it. I could spend my time creating custom sorting routines, but there's got to be a way to sort FileInfo objects that I'm overlooking. If I can't inherit from FileInfo, therefore can't add the IComparable interface to it, which seems the most elegant solution, I'm planning to create a__gc class MyFileInfo : public IComparable
that contains its own FileInfo class rather than inheriting from it, then just passes along the information needed from within FileInfo. This seems it may work, but would require typing dozens of little stub routines just to allow MyFileInfo to pretend to be an extended FileInfo. The reason I'm asking instead of just doing is that sorting files seems to be something that ought to be supported by default someplace. Any suggestions or ideas? -mike -www.channelmike.com -- modified at 13:40 Friday 6th January, 2006I think you can pass an IComparer to Sort(), just write your own FileInfoComparer.