Files
comp4959/labs/01/sort.rs
T

32 lines
891 B
Rust

use std::env;
use std::fs;
use std::io;
fn record(line: &str) -> Option<(String, String, u8)> {
let mut fields = line.split_whitespace();
let first_name = fields.next()?;
let last_name = fields.next()?;
let score: u8 = fields.next()?.parse().ok()?;
if score > 100 {
return None;
}
Some((first_name.to_string(), last_name.to_string(), score))
}
fn main() {
let input = match env::args().nth(1) {
Some(path) => fs::read_to_string(path).unwrap_or_default(),
None => io::read_to_string(io::stdin()).unwrap_or_default(),
};
let mut records: Vec<(String, String, u8)> = input.lines().filter_map(record).collect();
records.sort_by(|a, b| (a.2, &a.1, &a.0).cmp(&(b.2, &b.1, &b.0)));
records.dedup();
for (first_name, last_name, score) in records {
println!("{} {}, {}", score, last_name, first_name);
}
}