Sort_by on vec of string pairs

I have a vector of strings returned from the fastcgi::Request::params() function and I wish to sort it by the parameters.

       let v:Vec<(String, String)> = req.params().collect();
        for p in v.sort_by(|a,b| a.0.partial_cmp(&b.0).unwrap()) {

I get the error:

9 |         for p in v.sort_by(|a,b| a.0.partial_cmp(&b.0).unwrap()) {
  |                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `()` is not an iterator

I do not understand.

This is my complete main.rs

extern crate fastcgi;
use std::io::Write;
fn main() {
    fastcgi::run(|mut req| {
        let mut params = "".to_string();
        let v:Vec<(String, String)> = req.params().collect();
        for p in v.sort_by(|a,b| a.0.partial_cmp(&b.0).unwrap()) {
            params += format!("{} => {}\n", p.0, p.1).as_str();
        }
        write!(&mut req.stdout(), "Content-Type: text/plain\n\nHello, world!\n{}", params)
            .unwrap_or(());
    });
}

sort_by does not return an iterator. You can sort first, then iterate over v like this.

v.sort_by(|a,b| a.0.partial_cmp(&b.0).unwrap());
for p in v {
    params += format!("{} => {}\n", p.0, p.1).as_str();
}
2 Likes

sorts in place. Been here before, duh!
thanks.