about summary refs log tree commit diff
diff options
context:
space:
mode:
authorgennyble <gen@nyble.dev>2021-03-16 01:57:30 -0500
committergennyble <gen@nyble.dev>2021-03-16 01:57:30 -0500
commit4d9bab22ff41a7089e58516336005002a934cbd3 (patch)
tree3299a40474e56107c946479945cfa62d5ed7d647
parent97e113eedde3cd802a10eba8656ddb8e949ff985 (diff)
downloadcolorsquash-4d9bab22ff41a7089e58516336005002a934cbd3.tar.gz
colorsquash-4d9bab22ff41a7089e58516336005002a934cbd3.zip
Refactor color frequency sort
Rust is beautiful! Ordering::then is beautiful!
-rw-r--r--src/main.rs28
1 files changed, 1 insertions, 27 deletions
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<u8>, 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...");