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

1 Answer

0 votes
from dataclasses import dataclass

# ------------------------------------------------------------
# Date struct for proper date handling
# ------------------------------------------------------------
@dataclass
class Date:
    year: int
    month: int
    day: int

# ------------------------------------------------------------
# print a date in YYYY-MM-DD format
# ------------------------------------------------------------
def format_date(d: Date) -> str:
    return f"{d.year:04d}-{d.month:02d}-{d.day:02d}"

# ------------------------------------------------------------
# File entry struct
# ------------------------------------------------------------
@dataclass
class FileEntry:
    name: str          # file name without extension
    extension: str     # extension including dot
    date: Date         # real date struct
    size: int          # bytes

# ------------------------------------------------------------
# Sort by name (lexicographically)
# ------------------------------------------------------------
def sort_by_name(files):
    return sorted(files, key=lambda f: f.name)

# ------------------------------------------------------------
# Sort by extension (lexicographically)
# ------------------------------------------------------------
def sort_by_extension(files):
    return sorted(files, key=lambda f: f.extension)

# ------------------------------------------------------------
# Sort by date (year → month → day)
# ------------------------------------------------------------
def sort_by_date(files):
    return sorted(files, key=lambda f: (f.date.year, f.date.month, f.date.day))

# ------------------------------------------------------------
# Sort by size (ascending)
# ------------------------------------------------------------
def sort_by_size(files):
    return sorted(files, key=lambda f: f.size)

# ------------------------------------------------------------
# print file list
# ------------------------------------------------------------
def print_files(files):
    for f in files:
        print(f"{f.name}{f.extension}  {format_date(f.date)}  {f.size} bytes")
    print()

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

print("Original:")
print_files(files)

print("Sorted by name:")
print_files(sort_by_name(files))

print("Sorted by extension:")
print_files(sort_by_extension(files))

print("Sorted by date:")
print_files(sort_by_date(files))

print("Sorted by size:")
print_files(sort_by_size(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:
PDqmuO3GH.cpp  2004-05-21  3346 bytes
HqclTqxb4.cpp  2020-09-05  70531 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
qJO2qjukZ.png  2010-08-27  67087 bytes
0SnZHh2GT.png  2006-10-18  25890 bytes
rXwXdy8XO.txt  2017-10-12  70193 bytes
ZHahuu4vS.txt  2003-10-10  65126 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 22 hours ago by avibootz

Related questions

...