From 279fed9f175a84799f37ffb3fbb22b64313f8c47 Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 12 May 2020 16:12:33 +0200 Subject: sort players based on their names The easiest way to get this consistent is to implement PartialOrd & Ord for Player. However, the implementation might not be 100% sound, as it should be using the same fields as (Partial)Eq, but that would mean either 1. Deriving PartialOrd/Ord, which is not possible because PlayerClass does not implement it (which in turn would not make sense to order) or 2. Implementing (Partial)Eq by hand instead of deriving it, which is not the best either I don't think it's an issue though, as we never put Players in any position where it might get relevant (such as a HashMap/BTreeMap), and we're only using it to sort them for the output. --- src/main.rs | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index ff00732..40f434c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -143,7 +143,7 @@ pub struct LogResult { } /// A player. -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct Player { /// Account name of the player. account_name: String, @@ -157,6 +157,22 @@ pub struct Player { guild_id: Option, } +impl PartialOrd for Player { + fn partial_cmp(&self, other: &Player) -> Option { + Some(self.cmp(other)) + } +} + +impl Ord for Player { + fn cmp(&self, other: &Self) -> std::cmp::Ordering { + (self.subgroup, &self.account_name, &self.character_name).cmp(&( + other.subgroup, + &other.account_name, + &other.character_name, + )) + } +} + /// Outcome of the fight. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub enum FightOutcome { @@ -485,7 +501,7 @@ fn extract_info(entry: &DirEntry, log: &Log) -> LogResult { guild_id: guild_ids.get(&p.addr()).cloned(), }) .collect::>(); - players.sort_by_key(|p| p.subgroup); + players.sort(); LogResult { log_file: entry.path().to_path_buf(), -- cgit v1.2.3