The tracker's report lists the longest tasks first. Two tasks of 25 minutes raise a question that is easy to skip: which one comes first? If the answer is "whichever the sort happened to leave first", the report can change when the input order changes, a test that compares exact output fails on another compiler, and a user sees rows swap for no visible reason. A report is deterministic when the same set of tasks always produces the same text.
std::sort takes a comparator, a function or lambda comp(a, b) that returns true when a belongs before b. Getting deterministic output means two things: a comparator that obeys the rules std::sort relies on, and enough keys that no two different tasks compare as equal.
Predict and run
The comparator sorts by minutes, longest first, and breaks ties with the lower ID. Which IDs come out, in which order?
#include <algorithm>
#include <iostream>
#include <vector>
struct Row { int id; int minutes; };
int main() {
std::vector<Row> rows{{3,25},{1,10},{2,25}};
std::sort(rows.begin(), rows.end(), [](const Row& a, const Row& b) {
if (a.minutes != b.minutes) { return a.minutes > b.minutes; }
return a.id < b.id;
});
for (const Row& row : rows) { std::cout << row.id << "\n"; }
return 0;
}Check your prediction
2
3
1Tasks 3 and 2 both have 25 minutes, so the first test does not decide, and the second puts the lower ID first: 2, then 3. Task 1 has only 10 minutes and goes last. Only when the primary key ties does the comparator look at the secondary one.
The comparator contract
std::sort requires a strict weak ordering. In practice that means:
- Irreflexive:
comp(a, a)isfalse. Nothing comes before itself. - Asymmetric: if
comp(a, b)istrue, thencomp(b, a)isfalse. - Transitive: if
acomes beforebandbbeforec, thenacomes beforec.
Use < or >, never <= or >=. A comparator like this breaks the first rule:
std::sort(values.begin(), values.end(), [](int a, int b) { return a <= b; }); // Undefined behavior.This is not merely "a slightly different order". Implementations of std::sort use inner loops that move an iterator until the comparator returns false, and they skip bounds checks because a valid comparator guarantees that loop stops. With <=, a run of equal values can keep it moving past the end of the vector, reading and swapping memory that is not part of it. Whether that crashes depends on the data and the library version, so a passing test proves nothing. Do not run it; fix the comparator.
Ties and stability
std::sort is not stable: elements that compare equal may end up in any order, and that order can differ between library versions. std::stable_sort keeps equal elements in their input order, but for the tracker the input order is itself an accident of how the file was written. The robust fix is to make the comparator a total order over distinct tasks by ending with a key that is unique, the ID. Then no two different tasks are ever "equal", and the result no longer depends on the input order:
#include <algorithm>
#include <iostream>
#include <vector>
struct Row { int id; int minutes; };
bool report_before(const Row& a, const Row& b) {
if (a.minutes != b.minutes) { return a.minutes > b.minutes; }
return a.id < b.id;
}
void print_sorted(std::vector<Row> rows) {
std::sort(rows.begin(), rows.end(), report_before);
const char* separator = "";
for (const Row& row : rows) {
std::cout << separator << row.id << ":" << row.minutes;
separator = " ";
}
std::cout << "\n";
}
int main() {
print_sorted({{3, 25}, {1, 10}, {2, 25}, {4, 25}});
print_sorted({{4, 25}, {2, 25}, {1, 10}, {3, 25}});
return 0;
}2:25 3:25 4:25 1:10
2:25 3:25 4:25 1:10print_sorted takes its vector by value, so it sorts its own copy and the caller's data keeps its order. The tracker does the same: report_order sorts a snapshot, and the stored map stays in ID order for export.
Practice
Your turn
Write std::vector<Row> report(std::vector<Row> rows) for struct Row { int id; int minutes; bool done; }; that orders pending tasks before done ones, then longer before shorter, then lower ID first. For the input {5, 25, true}, {3, 25, false}, {1, 40, true}, {4, 10, false}, {2, 25, false}, print the result and confirm the input was not reordered:
2 (25, todo)
3 (25, todo)
4 (10, todo)
1 (40, done)
5 (25, done)
Input first id: 5#include <algorithm>
#include <iostream>
#include <vector>
struct Row { int id; int minutes; bool done; };
// Write report here.
int main() {
const std::vector<Row> rows{{5, 25, true}, {3, 25, false}, {1, 40, true}, {4, 10, false}, {2, 25, false}};
return 0;
}Hint 1: the idea
Compare one key at a time. Return as soon as a key differs; fall through to the next key only on a tie. The last key, the ID, is unique, so it always decides.
Hint 2: a first step
Start the comparator with if (a.done != b.done) { return !a.done; }: when exactly one is done, a comes first only if a is the pending one. Then copy the minutes and ID tests from the lesson.
Show one solution
#include <algorithm>
#include <iostream>
#include <vector>
struct Row { int id; int minutes; bool done; };
std::vector<Row> report(std::vector<Row> rows) {
std::sort(rows.begin(), rows.end(), [](const Row& a, const Row& b) {
if (a.done != b.done) { return !a.done; }
if (a.minutes != b.minutes) { return a.minutes > b.minutes; }
return a.id < b.id;
});
return rows;
}
int main() {
const std::vector<Row> rows{{5, 25, true}, {3, 25, false}, {1, 40, true}, {4, 10, false}, {2, 25, false}};
for (const Row& row : report(rows)) {
std::cout << row.id << " (" << row.minutes << ", " << (row.done ? "done" : "todo") << ")\n";
}
std::cout << "Input first id: " << rows.front().id << "\n";
return 0;
}2 (25, todo)
3 (25, todo)
4 (10, todo)
1 (40, done)
5 (25, done)
Input first id: 5Each key test uses != to detect a tie and a strict comparison to decide, so comp(a, a) is always false and the contract holds. Task 1 is the longest but appears late because the first key, the state, outranks minutes. The input is const and passed by value, so the sort could only touch the copy. The checkpoint next combines a predicate and this ordering into one filtered report.
Completion is saved in this browser only. It does not measure mastery.