From 4d9bab22ff41a7089e58516336005002a934cbd3 Mon Sep 17 00:00:00 2001 From: gennyble Date: Tue, 16 Mar 2021 01:57:30 -0500 Subject: Refactor color frequency sort Rust is beautiful! Ordering::then is beautiful! --- src/main.rs | 28 +--------------------------- 1 file changed, 1 insertion(+), 27 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 82d6a99..67ef397 100644 --- a/src/main.rs +++ b/src/main.rs @@ -36,33 +36,7 @@ fn main() { println!("{} has {} colors in it. Sorting most occuring to least...", filename, colors.len()); let mut sorted: Vec<(Rgb, usize)> = colors.into_iter().collect(); - sorted.sort_by(|a, b| { - match a.1.cmp(&b.1) { - Ordering::Equal => { - match a.0.0[0].cmp(&b.0.0[0]) { - Ordering::Equal => { - match a.0.0[1].cmp(&b.0.0[1]) { - Ordering::Equal => { - match a.0.0[2].cmp(&b.0.0[2]) { - Ordering::Equal => { - panic!("Same color in count map, what?") - }, - Ordering::Greater => Ordering::Less, - Ordering::Less => Ordering::Greater - } - }, - Ordering::Greater => Ordering::Less, - Ordering::Less => Ordering::Greater - } - }, - Ordering::Greater => Ordering::Less, - Ordering::Less => Ordering::Greater - } - }, - Ordering::Greater => Ordering::Less, - Ordering::Less => Ordering::Greater - } - }); + sorted.sort_by(|a, b| {a.1.cmp(&b.1).then(a.0.0.cmp(&b.0.0).reverse())}); println!("Sorted! Selecting colors..."); -- cgit 1.4.1-3-g733a5