lothos12345 Posted August 24, 2006 Posted August 24, 2006 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. Quote
Leaders snarfblam Posted August 24, 2006 Leaders Posted August 24, 2006 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 Quote [sIGPIC]e[/sIGPIC]
Cags Posted August 25, 2006 Posted August 25, 2006 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. Quote Anybody looking for a graduate programmer (Midlands, England)?
Leaders snarfblam Posted August 25, 2006 Leaders Posted August 25, 2006 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. Quote [sIGPIC]e[/sIGPIC]
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.