SortOption enum defines how files are ordered when displaying directory contents.
Overview
Sorting is controlled by thesortBy field in DisplayOptions. By default, files are sorted alphabetically by name, but you can also sort by modification time or file size.
Source: Sources/SwiftListCore/Configuration/SortOption.swift:13-22
Sort Options
Sort by Name (Default)
Sorts files alphabetically by filename using localized comparison. This is the default behavior. Enum value:SortOption.name
CLI usage: Default behavior (no flag needed)
localizedCompare for natural sorting order (e.g., “file2.txt” comes before “file10.txt”).
Source: Sources/SwiftListCore/Utilities/FileManagerHelper.swift:224-227
Sort by Time
Sorts files by modification time, with the most recently modified files appearing first. Enum value:SortOption.time
CLI flag: -t
- Finding recently modified files
- Tracking recent work
- Identifying stale files
contentModificationDate resource values. Files without modification dates are treated as having Date.distantPast.
Source: Sources/SwiftListCore/Utilities/FileManagerHelper.swift:228-233
Sort by Size
Sorts files by size, with the largest files appearing first. Enum value:SortOption.size
CLI flag: -S
- Finding large files consuming disk space
- Identifying files for cleanup
- Understanding storage usage patterns
fileSize resource values. Files without size information are treated as 0 bytes.
Source: Sources/SwiftListCore/Utilities/FileManagerHelper.swift:234-238
Combining Sort with Other Options
Sorting works seamlessly with all other display options:Sort with Recursion
Sort with Long Format
-l makes it easy to verify the sort order visually.
Sort with Filters
Sort Implementation Details
The sorting algorithm is applied inFileManagerHelper.contents() after retrieving directory contents:
Sources/SwiftListCore/Utilities/FileManagerHelper.swift:222-239
Performance Considerations
- Name sorting is the fastest, as it only uses filenames already in memory
- Time sorting requires reading modification date metadata for each file
- Size sorting requires reading size metadata for each file
Tips
When sorting by time or size, files without the requested metadata (e.g., files where metadata can’t be read) are treated as having:
- Time:
Date.distantPast(sorted to the end) - Size:
0bytes (sorted to the end)
Related
- Display Options - All configuration options
- Color and Icons - Visual file type indicators