How to sort a list of files by name, extension, dates, and size in VB.NET

1 Answer

0 votes
Imports System
Imports System.Collections.Generic

Public Class FileSorter

    ' ------------------------------------------------------------
    ' Date struct for proper date handling
    ' ------------------------------------------------------------
    Public Class [Date]
        Public Year As Integer
        Public Month As Integer
        Public Day As Integer

        Public Sub New(y As Integer, m As Integer, d As Integer)
            Year = y
            Month = m
            Day = d
        End Sub

        ' ------------------------------------------------------------
		' Print a date in YYYY-MM-DD format
        ' ------------------------------------------------------------
        Public Function FormatDate() As String
            Return String.Format("{0:0000}-{1:00}-{2:00}", Year, Month, Day)
        End Function
    End Class

    ' ------------------------------------------------------------
    ' File entry struct
    ' ------------------------------------------------------------
    Public Class FileEntry
        Public Name As String        ' file name without extension
        Public Extension As String   ' extension including dot
        Public FileDate As [Date]    ' real date struct
        Public Size As Integer       ' bytes

        Public Sub New(n As String, ext As String, d As [Date], s As Integer)
            Name = n
            Extension = ext
            FileDate = d
            Size = s
        End Sub
    End Class

    ' ------------------------------------------------------------
    ' Sort by name (lexicographically)
    ' ------------------------------------------------------------
    Public Shared Sub SortByName(files As List(Of FileEntry))
        files.Sort(Function(a, b) String.Compare(a.Name, b.Name, StringComparison.Ordinal))
    End Sub

    ' ------------------------------------------------------------
    ' Sort by extension (lexicographically)
    ' ------------------------------------------------------------
    Public Shared Sub SortByExtension(files As List(Of FileEntry))
        files.Sort(Function(a, b) String.Compare(a.Extension, b.Extension, StringComparison.Ordinal))
    End Sub

    ' ------------------------------------------------------------
    ' Sort by date (year → month → day)
    ' ------------------------------------------------------------
    Public Shared Sub SortByDate(files As List(Of FileEntry))
        files.Sort(Function(a, b)
                       If a.FileDate.Year <> b.FileDate.Year Then
                           Return a.FileDate.Year.CompareTo(b.FileDate.Year)
                       End If
                       If a.FileDate.Month <> b.FileDate.Month Then
                           Return a.FileDate.Month.CompareTo(b.FileDate.Month)
                       End If
                       Return a.FileDate.Day.CompareTo(b.FileDate.Day)
                   End Function)
    End Sub

    ' ------------------------------------------------------------
    ' Sort by size (ascending)
    ' ------------------------------------------------------------
    Public Shared Sub SortBySize(files As List(Of FileEntry))
        files.Sort(Function(a, b) a.Size.CompareTo(b.Size))
    End Sub

    ' ------------------------------------------------------------
	' Print file list
    ' ------------------------------------------------------------
    Public Shared Sub PrintFiles(files As List(Of FileEntry))
        For Each f In files
            Console.WriteLine($"{f.Name}{f.Extension}  {f.FileDate.FormatDate()}  {f.Size} bytes")
        Next
        Console.WriteLine()
    End Sub

    Public Shared Sub Main()

        Dim files As New List(Of FileEntry) From {
            New FileEntry("G1zTo5jJk", ".jpg", New [Date](2007, 7, 8), 51954),
            New FileEntry("LTEE4SI0j", ".jpg", New [Date](2011, 11, 13), 43442),
            New FileEntry("PDqmuO3GH", ".cpp", New [Date](2004, 5, 21), 3346),
            New FileEntry("qJO2qjukZ", ".png", New [Date](2010, 8, 27), 67087),
            New FileEntry("HqclTqxb4", ".cpp", New [Date](2020, 9, 5), 70531),
            New FileEntry("imVyTyoaF", ".jpg", New [Date](2011, 3, 19), 43846),
            New FileEntry("rXwXdy8XO", ".txt", New [Date](2017, 10, 12), 70193),
            New FileEntry("9Z4fbOBUc", ".pdf", New [Date](2004, 6, 9), 1754),
            New FileEntry("ZHahuu4vS", ".txt", New [Date](2003, 10, 10), 65126),
            New FileEntry("0SnZHh2GT", ".png", New [Date](2006, 10, 18), 25890)
        }

        Console.WriteLine("Original:")
        PrintFiles(files)

        Console.WriteLine("Sorted by name:")
        SortByName(files)
        PrintFiles(files)

        Console.WriteLine("Sorted by extension:")
        SortByExtension(files)
        PrintFiles(files)

        Console.WriteLine("Sorted by date:")
        SortByDate(files)
        PrintFiles(files)

        Console.WriteLine("Sorted by size:")
        SortBySize(files)
        PrintFiles(files)
    End Sub
End Class


' run:
'
' Original:
' G1zTo5jJk.jpg  2007-07-08  51954 bytes
' LTEE4SI0j.jpg  2011-11-13  43442 bytes
' PDqmuO3GH.cpp  2004-05-21  3346 bytes
' qJO2qjukZ.png  2010-08-27  67087 bytes
' HqclTqxb4.cpp  2020-09-05  70531 bytes
' imVyTyoaF.jpg  2011-03-19  43846 bytes
' rXwXdy8XO.txt  2017-10-12  70193 bytes
' 9Z4fbOBUc.pdf  2004-06-09  1754 bytes
' ZHahuu4vS.txt  2003-10-10  65126 bytes
' 0SnZHh2GT.png  2006-10-18  25890 bytes
'
' Sorted by name:
' 0SnZHh2GT.png  2006-10-18  25890 bytes
' 9Z4fbOBUc.pdf  2004-06-09  1754 bytes
' G1zTo5jJk.jpg  2007-07-08  51954 bytes
' HqclTqxb4.cpp  2020-09-05  70531 bytes
' LTEE4SI0j.jpg  2011-11-13  43442 bytes
' PDqmuO3GH.cpp  2004-05-21  3346 bytes
' ZHahuu4vS.txt  2003-10-10  65126 bytes
' imVyTyoaF.jpg  2011-03-19  43846 bytes
' qJO2qjukZ.png  2010-08-27  67087 bytes
' rXwXdy8XO.txt  2017-10-12  70193 bytes
'
' Sorted by extension:
' HqclTqxb4.cpp  2020-09-05  70531 bytes
' PDqmuO3GH.cpp  2004-05-21  3346 bytes
' G1zTo5jJk.jpg  2007-07-08  51954 bytes
' LTEE4SI0j.jpg  2011-11-13  43442 bytes
' imVyTyoaF.jpg  2011-03-19  43846 bytes
' 9Z4fbOBUc.pdf  2004-06-09  1754 bytes
' 0SnZHh2GT.png  2006-10-18  25890 bytes
' qJO2qjukZ.png  2010-08-27  67087 bytes
' ZHahuu4vS.txt  2003-10-10  65126 bytes
' rXwXdy8XO.txt  2017-10-12  70193 bytes
'
' Sorted by date:
' ZHahuu4vS.txt  2003-10-10  65126 bytes
' PDqmuO3GH.cpp  2004-05-21  3346 bytes
' 9Z4fbOBUc.pdf  2004-06-09  1754 bytes
' 0SnZHh2GT.png  2006-10-18  25890 bytes
' G1zTo5jJk.jpg  2007-07-08  51954 bytes
' qJO2qjukZ.png  2010-08-27  67087 bytes
' imVyTyoaF.jpg  2011-03-19  43846 bytes
' LTEE4SI0j.jpg  2011-11-13  43442 bytes
' rXwXdy8XO.txt  2017-10-12  70193 bytes
' HqclTqxb4.cpp  2020-09-05  70531 bytes
'
' Sorted by size:
' 9Z4fbOBUc.pdf  2004-06-09  1754 bytes
' PDqmuO3GH.cpp  2004-05-21  3346 bytes
' 0SnZHh2GT.png  2006-10-18  25890 bytes
' LTEE4SI0j.jpg  2011-11-13  43442 bytes
' imVyTyoaF.jpg  2011-03-19  43846 bytes
' G1zTo5jJk.jpg  2007-07-08  51954 bytes
' ZHahuu4vS.txt  2003-10-10  65126 bytes
' qJO2qjukZ.png  2010-08-27  67087 bytes
' rXwXdy8XO.txt  2017-10-12  70193 bytes
' HqclTqxb4.cpp  2020-09-05  70531 bytes
'

 



answered 23 hours ago by avibootz

Related questions

...