Stable sort with optional scores last
C++
#include <algorithm>
#include <optional>
#include <string>
#include <vector>
struct Row {
std::string name;
std::optional<int> score;
};
void sortRows(std::vector<Row>& rows) {
std::stable_sort(rows.begin(), rows.end(), [](const Row& a, const Row& b) {
if (a.score.has_value() != b.score.has_value()) return a.score.has_value();
if (a.score && *a.score != *b.score) return *a.score > *b.score;
return a.name < b.name;
});
}