Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. Sorting Files in .NET

Sorting Files in .NET

Scheduled Pinned Locked Moved .NET (Core and Framework)
csharpc++visual-studiocomalgorithms
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mightypile
    wrote on last edited by
    #1

    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, then files->Add(someFileInfoObject) to populate the array. This all works well and I get an accurate list of files. But files->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

    D D 2 Replies Last reply
    0
    • M mightypile

      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, then files->Add(someFileInfoObject) to populate the array. This all works well and I get an accurate list of files. But files->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

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      Sort 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

      1 Reply Last reply
      0
      • M mightypile

        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, then files->Add(someFileInfoObject) to populate the array. This all works well and I get an accurate list of files. But files->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

        D Offline
        D Offline
        Daniel Grunwald
        wrote on last edited by
        #3

        I think you can pass an IComparer to Sort(), just write your own FileInfoComparer.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups