Skip to content

Commit

Permalink
Fixed final output order with stable sort
Browse files Browse the repository at this point in the history
  • Loading branch information
nickgerace committed Apr 5, 2021
1 parent 6d0a614 commit ba1c71e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
<!-- The latest version contains all changes. -->

### Changed

- Fixed final output order (sorted by name, then by status)

## [1.0.3] - 2021-04-02

Expand Down
11 changes: 6 additions & 5 deletions gfld/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -114,9 +114,10 @@ pub fn run(path: &Path) -> Result<(), Box<dyn Error>> {
}

// 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(
Expand All @@ -130,7 +131,7 @@ pub fn run(path: &Path) -> Result<(), Box<dyn Error>> {
);

// Despite using "println!" N times, this loop has minimal impact on runtime performance.
for outcome in vec {
for outcome in &vec {
println!(
"{:<width_name$} {:<width_status$} {:<width_branch$} {:<width_source$}",
outcome.name,
Expand Down

0 comments on commit ba1c71e

Please sign in to comment.