Directory Iterators

Sort Directory Entries

Is it possible to sort the directory entries while iterating?

Abstract art representing computer programming

While std::filesystem::directory_iterator does not directly support sorting, you can collect the directory entries into a container (like std::vector), sort the container, and then iterate over it.

Here’s an example of how to sort directory entries by their file names:

#include <filesystem>
#include <iostream>
#include <vector>
#include <algorithm>

namespace fs = std::filesystem;

int main() {
  fs::directory_iterator start{R"(c:\test)"};
  fs::directory_iterator end{};

  std::vector<fs::directory_entry> entries; 
  for (auto iter{start}; iter != end; ++iter) {
    entries.push_back(*iter);
  }

  std::sort(entries.begin(), entries.end(), 
    [](const fs::directory_entry& a,
      const fs::directory_entry& b) {
      return a.path().filename()
        < b.path().filename(); 
  });

  for (const auto& entry : entries) {
    std::cout << entry.path().string() << '\n';
  }
}
c:\test\another_directory
c:\test\file1.txt
c:\test\file2.txt

Steps to Sort Entries:

  1. Collect Entries: Use a loop to gather directory entries into a container like std::vector.
  2. Sort Container: Use std::sort() with a custom comparator to sort the entries based on desired criteria (e.g., filename).
  3. Iterate Sorted Container: Iterate over the sorted container and process the entries.

Sorting by Different Criteria:

You can sort by other criteria, such as file size or modification time:

std::sort(entries.begin(), entries.end(),
  [](const fs::directory_entry& a,
     const fs::directory_entry& b) {
    return fs::file_size(a) < fs::file_size(b); 
  });

Using Range-Based Algorithms:

You can also leverage std::ranges for a more modern approach:

#include <filesystem>
#include <iostream>
#include <vector>
#include <ranges>

namespace fs = std::filesystem;

int main() {
  auto entries = std::ranges::subrange{
    fs::directory_iterator{R"(c:\test)"},
    fs::directory_iterator{}
  } | std::ranges::to<std::vector>();

  std::ranges::sort(
    entries, {}, &fs::directory_entry::path);

  for (const auto& entry : entries) {
    std::cout << entry.path().string() << '\n';
  }
}

Sorting directory entries provides greater flexibility and control over how you process files and directories. By collecting entries into a container and sorting them, you can achieve any custom order needed for your application.

This Question is from the Lesson:

Directory Iterators

An introduction to iterating through the file system, using directory iterators and recursive directory iterators

Answers to questions are automatically generated and may not have been reviewed.

This Question is from the Lesson:

Directory Iterators

An introduction to iterating through the file system, using directory iterators and recursive directory iterators

A computer programmer
Part of the course:

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Free, unlimited access

This course includes:

  • 125 Lessons
  • 550+ Code Samples
  • 96% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved