Sort a vector with a comparator which changes its

2020-05-06 09:02发布

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?

1条回答
Animai°情兽
2楼-- · 2020-05-06 09:24

How do I dynamically generate that function

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 was Equal:

use std::cmp::Ordering;

#[derive(Debug, Copy, Clone)]
enum Field {
    Artist,
    Title,
    Date,
}

struct TheStruct {
    artist: String,
    title: String,
    date: String,
}

fn main() {
    let mut items: Vec<TheStruct> = vec![];

    use Field::*;
    let orders = vec![Artist, Title];

    items.sort_by(|a, b| {
        orders.iter().fold(Ordering::Equal, |acc, &field| {
            acc.then_with(|| {
                match field {
                    Artist => a.artist.cmp(&b.artist),
                    Title => a.title.cmp(&b.title),
                    Date => a.date.cmp(&b.date),
                }
            })
        })
    })
}

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.

查看更多
登录 后发表回答