From 3774d280bd1e4a1f4da78d5a6e78aca58f6908a7 Mon Sep 17 00:00:00 2001 From: Simon Gardling Date: Mon, 4 Apr 2022 10:26:05 -0400 Subject: [PATCH] sort autocompletes from smallest to largest --- src/autocomplete_helper.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/autocomplete_helper.rs b/src/autocomplete_helper.rs index 73da4af..f11fd9f 100644 --- a/src/autocomplete_helper.rs +++ b/src/autocomplete_helper.rs @@ -1,5 +1,17 @@ +use core::cmp::Ordering; use std::collections::HashSet; +/// https://www.dotnetperls.com/sort-rust +fn compare_len_reverse_alpha(a: &String, b: &String) -> Ordering { + // Sort by length from short to long first. + let length_test = a.len().cmp(&b.len()); + if length_test == Ordering::Equal { + // If same length, sort in reverse alphabetical order. + return b.cmp(&a); + } + return length_test; +} + /// Generates hashmap (well really a vector of tuple of strings that are then /// turned into a hashmap by phf) #[allow(dead_code)] @@ -28,11 +40,12 @@ pub fn compile_hashmap(data: Vec) -> Vec<(String, String)> { if keys.iter().filter(|a| a == &&key).count() == 1 { output.push((key.clone(), format!(r#"HintEnum::Single("{}")"#, value))); } else { - let multi_data = tuple_list_1 + let mut multi_data = tuple_list_1 .iter() .filter(|(a, _)| a == key) .map(|(_, b)| b) .collect::>(); + multi_data.sort_unstable_by(|a, b| compare_len_reverse_alpha(a, b)); output.push((key.clone(), format!("HintEnum::Many(&{:?})", multi_data))); } }