From: Scott Worley Date: Tue, 20 Aug 2024 17:38:24 +0000 (-0700) Subject: Sort columns _descending_ by frequency X-Git-Tag: v0.2.1~2 X-Git-Url: http://git.scottworley.com/tablify/commitdiff_plain/38d1167ae3ec3dac0f183938bb353c995052ae6c?ds=sidebyside;hp=3bc643e943293ff1a352ca554797a274f2bc91ae Sort columns _descending_ by frequency --- 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"))] ); }