diff options
author | Daniel <kingdread@gmx.de> | 2020-05-12 16:12:33 +0200 |
---|---|---|
committer | Daniel <kingdread@gmx.de> | 2020-05-12 16:12:33 +0200 |
commit | 279fed9f175a84799f37ffb3fbb22b64313f8c47 (patch) | |
tree | e45e7050171ada617ba93a6ee818b6fe93cf326f | |
parent | 013776cbc4ebf6242e64df25d3bebf4851de116b (diff) | |
download | raidgrep-279fed9f175a84799f37ffb3fbb22b64313f8c47.tar.gz raidgrep-279fed9f175a84799f37ffb3fbb22b64313f8c47.tar.bz2 raidgrep-279fed9f175a84799f37ffb3fbb22b64313f8c47.zip |
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.
-rw-r--r-- | src/main.rs | 20 |
1 files 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<String>, } +impl PartialOrd for Player { + fn partial_cmp(&self, other: &Player) -> Option<std::cmp::Ordering> { + 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::<Vec<Player>>(); - players.sort_by_key(|p| p.subgroup); + players.sort(); LogResult { log_file: entry.path().to_path_buf(), |