4

Get sorted date in vb

 3 years ago
source link: https://www.codesd.com/item/get-sorted-date-in-vb.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Get sorted date in vb

advertisements

i am writing simple program to get the sorted date . But it does not work.

Dim filepath As String = FileStr
Dim directoryPath As String =      System.IO.Path.GetDirectoryName(filepath)
for Each file As String In System.IO.Directory.GetFiles(directoryPath)
Dates = {
Date.Parse(System.IO.Path.GetFileNameWithoutExtension(file))
        }.ToList
Next
Dates.Sort()
ComboBox1.DataSource = Dates

It only show one date ..where there are more than 10 date. and also the loop is working

I declare the List as global


You're replacing the content of your list in every loop

Dim filepath As String = FileStr
Dim directoryPath As String =      System.IO.Path.GetDirectoryName(filepath)
for Each file As String In System.IO.Directory.GetFiles(directoryPath)
    Dates.Add(Date.Parse(System.IO.Path.GetFileNameWithoutExtension(file)))
Next
Dates.Sort(AddressOf SortDate)
ComboBox1.DataSource = Dates


UPDATE: Sorting issue

Then as a seperate function add:

Function SortDate(ByVal a As DateTime, ByVal b As DateTime)
    Dim result As Integer = a.Year.CompareTo(b.Year)
    If result = 0 Then
        a.Month.CompareTo(b.Month)
        If result = 0 Then
            a.Day.CompareTo(b.Day)
        End If
    End If
    Return result
End Function


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK