From 38d1167ae3ec3dac0f183938bb353c995052ae6c Mon Sep 17 00:00:00 2001 From: Scott Worley Date: Tue, 20 Aug 2024 10:38:24 -0700 Subject: [PATCH 1/1] Sort columns _descending_ by frequency --- Changelog | 1 + src/lib.rs | 8 ++++---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/Changelog b/Changelog index 4be743b..8af8811 100644 --- a/Changelog +++ b/Changelog @@ -1,5 +1,6 @@ ## [Unreleased] - A little more space up top +- Sort columns _descending_ by frequency ## [0.2.0] - 2024-08-19 Initial release diff --git a/src/lib.rs b/src/lib.rs index 3002692..fbafcb1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -137,7 +137,7 @@ fn column_counts(rows: &[RowInput]) -> Vec<(usize, String)> { .into_iter() .map(|(col, n)| (n, col)) .collect(); - counts.sort(); + counts.sort_unstable_by(|(an, acol), (bn, bcol)| bn.cmp(an).then(acol.cmp(bcol))); counts } fn column_order(rows: &[RowInput]) -> Vec { @@ -353,7 +353,7 @@ mod tests { .collect::, _>>() .unwrap() ), - vec![(1, String::from("bar")), (2, String::from("baz"))] + vec![(2, String::from("baz")), (1, String::from("bar"))] ); assert_eq!( column_counts( @@ -361,7 +361,7 @@ mod tests { .collect::, _>>() .unwrap() ), - vec![(1, String::from("bar")), (2, String::from("baz"))] + vec![(2, String::from("baz")), (1, String::from("bar"))] ); assert_eq!( column_counts( @@ -369,7 +369,7 @@ mod tests { .collect::, _>>() .unwrap() ), - vec![(1, String::from("bar")), (2, String::from("baz"))] + vec![(2, String::from("baz")), (1, String::from("bar"))] ); } -- 2.44.1