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

1 Answer

0 votes
using System;
using System.Collections.Generic;

public class FileSorter
{
    /* ------------------------------------------------------------ */
    /* Date struct for proper date handling                         */
    /* ------------------------------------------------------------ */
    public class Date
    {
        public int Year;
        public int Month;
        public int Day;

        public Date(int y, int m, int d)
        {
            Year = y;
            Month = m;
            Day = d;
        }

        /* ------------------------------------------------------------ */
        /* Print a date in YYYY-MM-DD format                     */
        /* ------------------------------------------------------------ */
        public string Format()
        {
            return $"{Year:0000}-{Month:00}-{Day:00}";
        }
    }

    /* ------------------------------------------------------------ */
    /* File entry struct                                             */
    /* ------------------------------------------------------------ */
    public class FileEntry
    {
        public string Name;       // file name without extension
        public string Extension;  // extension including dot
        public Date FileDate;     // real date struct
        public int Size;          // bytes

        public FileEntry(string name, string ext, Date date, int size)
        {
            Name = name;
            Extension = ext;
            FileDate = date;
            Size = size;
        }
    }

    /* ------------------------------------------------------------ */
    /* Sort by name (lexicographically)                              */
    /* ------------------------------------------------------------ */
    public static void SortByName(List<FileEntry> files)
    {
        files.Sort((a, b) => string.Compare(a.Name, b.Name, StringComparison.Ordinal));
    }

    /* ------------------------------------------------------------ */
    /* Sort by extension (lexicographically)                         */
    /* ------------------------------------------------------------ */
    public static void SortByExtension(List<FileEntry> files)
    {
        files.Sort((a, b) => string.Compare(a.Extension, b.Extension, StringComparison.Ordinal));
    }

    /* ------------------------------------------------------------ */
    /* Sort by date (year → month → day)                             */
    /* ------------------------------------------------------------ */
    public static void SortByDate(List<FileEntry> files)
    {
        files.Sort((a, b) =>
        {
            int cmp = a.FileDate.Year.CompareTo(b.FileDate.Year);
            if (cmp != 0) return cmp;

            cmp = a.FileDate.Month.CompareTo(b.FileDate.Month);
            if (cmp != 0) return cmp;

            return a.FileDate.Day.CompareTo(b.FileDate.Day);
        });
    }

    /* ------------------------------------------------------------ */
    /* Sort by size (ascending)                                      */
    /* ------------------------------------------------------------ */
    public static void SortBySize(List<FileEntry> files)
    {
        files.Sort((a, b) => a.Size.CompareTo(b.Size));
    }

    /* ------------------------------------------------------------ */
    /* Print file list                                       */
    /* ------------------------------------------------------------ */
    public static void PrintFiles(List<FileEntry> files)
    {
        foreach (var f in files) {
            Console.WriteLine($"{f.Name}{f.Extension}  {f.FileDate.Format()}  {f.Size} bytes");
        }
        Console.WriteLine();
    }

    public static void Main()
    {
        var files = new List<FileEntry>
        {
            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);
    }
}



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

Related questions

...