Arraylist

lothos12345

Well-known member
Joined
May 2, 2002
Messages
294
Location
Texas
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.
 
Why not create a structure to hold this information...
[Vb]
Structure FileInfo
Dim Filename As String
Dim CreationDate As DateTime
End Structure
[/VB]
And an IComparer that can be used with the ArrayList.Sort function
[VB]
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
[/VB]
 
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, Im just providing an alternative.
 
IComparer isnt 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.
 
Back
Top