Jump to content
Xtreme .Net Talk

Recommended Posts

Posted
I have a visual basic.net application I have files in a folder and for each file in the directory I want too add them to an arraylist. But I want them to be sorted from top to bottom based on there creation times. How can I accomplish this? Any help offerred is greatly appreciated.
  • Leaders
Posted

Why not create a structure to hold this information...

[Vb]

Structure FileInfo

Dim Filename As String

Dim CreationDate As DateTime

End Structure

[/code]

And an IComparer that can be used with the ArrayList.Sort function

Class CreationDateSorter
   Implements IComparer

   Function Compare(ByVal a As Object, ByVal b As Object) As Integer Implements IComparer.Compare
       Return DirectCast(a, FileInfo).CreationDate.CompareTo(DirectCast(b, FileInfo).CreationDate)
   End Function
End Class

[sIGPIC]e[/sIGPIC]
Posted
Whilst still using the structure an alternative to the IComparer would be to add the files to the arraylist in order i.e for each file iterate through the array untill the date of the current file is greater than the one at the current index in the array. The IComparer class is certainly a better solution, I'm just providing an alternative.
Anybody looking for a graduate programmer (Midlands, England)?
  • Leaders
Posted
IComparer isn't necessarily a better solution, Cags. Your solution will work approximately equally fast. IComparer is quicker and easier to write, but your solution will be more scalable because it would not require a complete re-sort if, say, we were to add files from another directory to the ArrayList. But I would have to say that to gain maximum efficiency (CPU-wise) a LinkedList or BinaryTree would be best.
[sIGPIC]e[/sIGPIC]

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...