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

1 Answer

0 votes
# ------------------------------------------------------------
# Date struct for proper date handling
# ------------------------------------------------------------
class DateStruct
  attr_accessor :year, :month, :day

  def initialize(year, month, day)
    @year  = year
    @month = month
    @day   = day
  end
end

# ------------------------------------------------------------
# Print a date in YYYY-MM-DD format
# ------------------------------------------------------------
def format_date(d)
  "%04d-%02d-%02d" % [d.year, d.month, d.day]
end

# ------------------------------------------------------------
# File entry struct
# ------------------------------------------------------------
class FileEntry
  attr_accessor :name, :extension, :date, :size

  def initialize(name, extension, date, size)
    @name      = name      # File name without extension
    @extension = extension # Extension including dot
    @date      = date      # Real date struct
    @size      = size      # Bytes
  end
end

# ------------------------------------------------------------
# Sort by name (lexicographically)
# ------------------------------------------------------------
def sort_by_name(files)
  files.sort_by { |f| f.name }
end

# ------------------------------------------------------------
# Sort by extension (lexicographically)
# ------------------------------------------------------------
def sort_by_extension(files)
  files.sort_by { |f| f.extension }
end

# ------------------------------------------------------------
# Sort by date (year → month → day)
# ------------------------------------------------------------
def sort_by_date(files)
  files.sort_by { |f| [f.date.year, f.date.month, f.date.day] }
end

# ------------------------------------------------------------
# Sort by size (ascending)
# ------------------------------------------------------------
def sort_by_size(files)
  files.sort_by { |f| f.size }
end

# ------------------------------------------------------------
# Print file list
# ------------------------------------------------------------
def print_files(files)
  files.each do |f|
    puts "#{f.name}#{f.extension}  #{format_date(f.date)}  #{f.size} bytes"
  end
  puts
end

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

puts "Original:"
print_files(files)

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

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

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

puts "Sorted by size:"
print_files(sort_by_size(files))




=begin
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

=end

 



answered 13 hours ago by avibootz

Related questions

...