From ba1c71e41b13ad505e27819d8050b52fbd4688f4 Mon Sep 17 00:00:00 2001 From: Nick Gerace Date: Sun, 4 Apr 2021 21:50:04 -0400 Subject: [PATCH] Fixed final output order with stable sort --- CHANGELOG.md | 6 +++++- gfld/src/lib.rs | 11 ++++++----- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e8d8d1f..bdc02db 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), ## [Unreleased] -The latest version contains all changes. + + +### Changed + +- Fixed final output order (sorted by name, then by status) ## [1.0.3] - 2021-04-02 diff --git a/gfld/src/lib.rs b/gfld/src/lib.rs index 9d4d431..9a9a416 100644 --- a/gfld/src/lib.rs +++ b/gfld/src/lib.rs @@ -10,7 +10,7 @@ use walkdir::WalkDir; const UNKNOWN: &str = "unknown"; -#[derive(Debug, Eq, PartialEq, Ord, PartialOrd)] +#[derive(Eq, PartialEq, Ord, PartialOrd)] struct Outcome { name: String, status: String, @@ -114,9 +114,10 @@ pub fn run(path: &Path) -> Result<(), Box> { } // Imperceptible time savings without either of these sorts. We want to sort by the first - // field in "Outcome", and then by the status. - vec.sort_unstable(); - vec.sort_unstable_by_key(|k| k.status.clone()); + // field in "Outcome", and then by the status. Our first sort can be unstable, but our second + // has to be stable to retain the original order. + vec.sort_unstable_by_key(|k| k.name.clone()); + vec.sort_by_key(|k| k.status.clone()); // Need to insert header after the sort is finished. vec.insert( @@ -130,7 +131,7 @@ pub fn run(path: &Path) -> Result<(), Box> { ); // Despite using "println!" N times, this loop has minimal impact on runtime performance. - for outcome in vec { + for outcome in &vec { println!( "{: