How to sort a list of files by name, extension, dates, and size in Go

1 Answer

0 votes
package main

import (
    "fmt"
    "sort"
)

// ------------------------------------------------------------
// Date struct for proper date handling
// ------------------------------------------------------------
type Date struct {
    Year  int
    Month int
    Day   int
}

// ------------------------------------------------------------
// Print a date in YYYY-MM-DD format
// ------------------------------------------------------------
func FormatDate(d Date) string {
    return fmt.Sprintf("%04d-%02d-%02d", d.Year, d.Month, d.Day)
}

// ------------------------------------------------------------
// File entry struct
// ------------------------------------------------------------
type FileEntry struct {
    Name      string // File name without extension
    Extension string // Extension including dot
    Date      Date   // Real date struct
    Size      int    // Bytes
}

// ------------------------------------------------------------
// Sort by name (lexicographically)
// ------------------------------------------------------------
func SortByName(files []FileEntry) {
    sort.Slice(files, func(i, j int) bool {
        return files[i].Name < files[j].Name
    })
}

// ------------------------------------------------------------
// Sort by extension (lexicographically)
// ------------------------------------------------------------
func SortByExtension(files []FileEntry) {
    sort.Slice(files, func(i, j int) bool {
        return files[i].Extension < files[j].Extension
    })
}

// ------------------------------------------------------------
// Sort by date (year → month → day)
// ------------------------------------------------------------
func SortByDate(files []FileEntry) {
    sort.Slice(files, func(i, j int) bool {
        a, b := files[i].Date, files[j].Date
        if a.Year != b.Year {
            return a.Year < b.Year
        }
        if a.Month != b.Month {
            return a.Month < b.Month
        }
        return a.Day < b.Day
    })
}

// ------------------------------------------------------------
// Sort by size (ascending)
// ------------------------------------------------------------
func SortBySize(files []FileEntry) {
    sort.Slice(files, func(i, j int) bool {
        return files[i].Size < files[j].Size
    })
}

// ------------------------------------------------------------
// Print file list
// ------------------------------------------------------------
func PrintFiles(files []FileEntry) {
    for _, f := range files {
        fmt.Printf("%s%s  %s  %d bytes\n",
            f.Name, f.Extension, FormatDate(f.Date), f.Size)
    }
    fmt.Println()
}

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

    fmt.Println("Original:")
    PrintFiles(files)

    fmt.Println("Sorted by name:")
    SortByName(files)
    PrintFiles(files)

    fmt.Println("Sorted by extension:")
    SortByExtension(files)
    PrintFiles(files)

    fmt.Println("Sorted by date:")
    SortByDate(files)
    PrintFiles(files)

    fmt.Println("Sorted by size:")
    SortBySize(files)
    PrintFiles(files)
}



/* 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 17 hours ago by avibootz

Related questions

...