For each in order of Sort?

J

jc

I have this code that was suppose to return images in a sorted order.
It does not because the for each is not coming back in the order of
the sorted "files" collection.


Imports System.Collections
Imports System.IO

Partial Class _Default
Inherits System.Web.UI.Page


Sub Page_Load()
Dim pictures As New Hashtable
Dim filename As String = ""
Dim files As FileInfo() = New DirectoryInfo("c:\jjj\ppp
\pictures\").GetFiles()
Array.Sort(Of FileInfo)(files, AddressOf FileInfoComparison)

For Each fi As FileInfo In files
filename = fi.Name.ToLower()
If filename.EndsWith(".jpg") Then
pictures("pictures/" & filename) = "thumb.aspx?
src=pictures/" & filename
End If
Next

Repeater1.DataSource = pictures
Page.DataBind()


End Sub


Private Shared Function FileInfoComparison(ByVal fi1 As FileInfo,
ByVal fi2 As FileInfo) As Integer
'Return Date.Compare(fi1.LastWriteTime, fi2.LastWriteTime)
Return String.Compare(fi1.Name, fi2.Name)
End Function



any way around this? Thanks for any help or information!
 
L

Lloyd Sheen

jc said:
I have this code that was suppose to return images in a sorted order.
It does not because the for each is not coming back in the order of
the sorted "files" collection.


Imports System.Collections
Imports System.IO

Partial Class _Default
Inherits System.Web.UI.Page


Sub Page_Load()
Dim pictures As New Hashtable
Dim filename As String = ""
Dim files As FileInfo() = New DirectoryInfo("c:\jjj\ppp
\pictures\").GetFiles()
Array.Sort(Of FileInfo)(files, AddressOf FileInfoComparison)

For Each fi As FileInfo In files
filename = fi.Name.ToLower()
If filename.EndsWith(".jpg") Then
pictures("pictures/" & filename) = "thumb.aspx?
src=pictures/" & filename
End If
Next

Repeater1.DataSource = pictures
Page.DataBind()


End Sub


Private Shared Function FileInfoComparison(ByVal fi1 As FileInfo,
ByVal fi2 As FileInfo) As Integer
'Return Date.Compare(fi1.LastWriteTime, fi2.LastWriteTime)
Return String.Compare(fi1.Name, fi2.Name)
End Function



any way around this? Thanks for any help or information!

This was answered before. You are sorting which is good. Then you put
the elements into a HashTable which looses the sort.

LS
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top