Rust multi-key stable sort with nulls last
Rust
#[derive(Debug, Clone)]
struct Row {
name: String,
score: Option<u32>,
active: bool,
}
fn sorted(mut rows: Vec<Row>) -> Vec<Row> {
rows.sort_by(|a, b| {
b.active
.cmp(&a.active)
.then_with(|| a.score.is_none().cmp(&b.score.is_none()))
.then_with(|| b.score.cmp(&a.score))
.then_with(|| a.name.cmp(&b.name))
});
rows
}