I have a vector of a custom struct and a list of attributes to use for ordering that vector in descending priority. For example:
struct TheStruct {
artist: String,
title: String,
date: String,
}
let order_vec: Vec<String> = vec!["artist".to_string(),"title".to_string(),"date".to_string()];
let item_vec: Vec<TheStruct> = Vec::new();
I want the vector to be ordered as given by order_vec
. In this example, it should first be ordered by the artists name, when this is equal it should be ordered by the title. I do not want to hard-code this ordering as order_vec
changes dynamically.
I found Vec::sort_by
which takes a compare function. How do I dynamically generate that function? Is there a way to do this without sort_by
?
You don't. You have a specific closure that has dynamic behavior inside of it.
Here, we have a list of sorts to apply. When we need to compare two items, we iterate through the list. We use
Ordering::then_with
to only apply the comparison when the previous comparison wasEqual
:I used an enum for the fields because I didn't want to deal with what to do when one of the sorts is an unknown field.