Skip to content
C++ Basics: Sort References Without Reordering the Original Records

C++ Basics: Sort References Without Reordering the Original Records

November 8, 2026

Build a sortable list of reference_wrapper objects to view records in another order without copying them.

Prepare a scratch directory

Tested on Linux x86_64 with GCC 14.2.0. You need a C++20-capable compiler and standard-library development headers, with g++ on PATH, plus Bash, a text editor, and the mktemp, rm, and rmdir utilities. No third-party library is required.

Keep the same terminal open. Stop if setup fails, before saving the source:

start_directory="$PWD"
work_directory="$(mktemp -d)" || exit 1
cd -- "$work_directory" || exit 1

Run the example

Save this as reference-wrapper-sort-view-demo.cpp:

#include <algorithm>
#include <functional>
#include <iostream>
#include <string>
#include <vector>

struct Job { std::string name; int priority; };

int main()
{
    std::vector<Job> jobs{{"backup", 3}, {"verify", 1}, {"index", 2}};
    std::vector<std::reference_wrapper<Job>> ordered;
    for (auto& job : jobs) ordered.push_back(std::ref(job));
    std::sort(ordered.begin(), ordered.end(), [](const auto& left, const auto& right) {
        return left.get().priority < right.get().priority;
    });
    ordered.front().get().name = "verify-now";
    std::cout << "view:";
    for (auto job : ordered) std::cout << ' ' << job.get().name;
    std::cout << "\nowner:";
    for (const auto& job : jobs) std::cout << ' ' << job.name;
    std::cout << '\n';
}

Compile and run it. The program runs only after compilation succeeds:

g++ -std=c++20 -Wall -Wextra -Werror -pedantic reference-wrapper-sort-view-demo.cpp -o reference-wrapper-sort-view-demo &&
./reference-wrapper-sort-view-demo

Observed output:

view: verify-now index backup
owner: backup verify-now index

Read the result

The view is ordered by priority, while jobs retains its original order. Renaming the first viewed job changes the second original record because the wrapper refers to that record rather than holding a copy.

Apply it carefully

reference_wrapper does not own its target. The jobs vector must outlive the view, and operations that invalidate references, such as a vector reallocation, also invalidate these wrappers. Changing priorities later does not automatically re-sort the view. std::sort does not preserve the order of equal-priority entries.

The C++ working draft describes the relevant rules. This example uses C++20; the current working draft also includes later additions.

Clean up

Remove only the source and executable created here, then return to the starting directory:

rm -- reference-wrapper-sort-view-demo.cpp reference-wrapper-sort-view-demo
cd -- "$start_directory" || exit 1
rmdir -- "$work_directory"
unset start_directory work_directory

If the directory is not empty, inspect its remaining contents before removing anything else.

Acknowledgements

This post was created with assistance from AI.

Last updated on