Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sort folder contents in ASP.Net

Status
Not open for further replies.

lpatnaik

Programmer
Jul 11, 2002
53
Can anyone tell me how to sort files in a folder according to the creation time or modification time through asp.net
As of now, i am just fetchning and displaying the file names. But i need to display them in a sorted order, latest files first.
My code :

Dim objDirInfo As DirectoryInfo
Dim arrFiles() As String
objDirInfo = New DirectoryInfo(strPath)

If objDirInfo.Exists = True Then
arrFiles = Directory.GetFiles(strPath)
 
A simple bubble sort of a multi-dimensional array would suffice.

If you wanted to, you could also place the values into a dataview, and use the .sort() method of the dataview to sort them.

No matter what you choose, though, you're going to have to pull the information out, place it into some sort of intermediary object, and do your sorting on that.

There are millions of sort examples out there. I'd suggest google as a starting place.
penny1.gif
penny1.gif
 
This works for me in constructing an alternate index of words that appear in a record. Perhaps you can see how to modify it to work for files. See System.Array.Sort. If you want to sort on different keys at different times then you must use the .Sort(array, IComparer) method which I have not researched.

Define a Structure for the File Entry implementing IComparable interface, create an array of the structures then sort it. How you compare dates etc is up to you.

'*********
Friend Structure Word
Implements IComparable
Dim Wrd_Type As String
Dim Wrd_Root_Type As String
Dim Wrd_Word As String
Dim Wrd_Root_Rest As String
Function CompareTo(ByVal B As Object) As Integer Implements IComparable.CompareTo
' Return is -1 for <, 0 for = and 1 for >
Dim Y As Word
Y = CType(B, Word)
Dim intResult As Integer
intResult = String.Compare(Wrd_Type, Y.Wrd_Type, True)
If intResult <> 0 Then Return intResult
intResult = String.Compare(Wrd_Root_Type, Y.Wrd_Root_Type, True)
If intResult <> 0 Then Return intResult
intResult = String.Compare(Wrd_Word, Y.Wrd_Word, True)
If intResult <> 0 Then Return intResult
Return String.Compare(Wrd_Root_Rest, Y.Wrd_Root_Rest, True)
End Function
End Structure
'***********************************
'
Dim aryOldWords As Word()
aryOldWords = GetKeyWordsForRecord(objBefore)
aryOldWords.Sort(aryOldWords)
Forms/Controls Resizing/Tabbing Control
Compare Code (Text)
Generate Sort Class in VB or VBScript
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top