Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Prodentim Probiotics Specially Designed For The Health Of Your Teeth And Gums

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Teach Your Child To Read

Powerful WordPress hosting for WordPress professionals

Disclosure: My content contains affiliate links.

31,038 questions

40,790 answers

573 users

How to sort a vector of structs in Rust

1 Answer

0 votes
#[derive(Debug, Eq, Ord, PartialEq, PartialOrd)]

struct Worker {
   name: String,
   company: String,
   age:u32
}

impl Worker {
    pub fn new(name: String, company: String, age: u32) -> Self {
        Worker {
            name,
            company,
            age
        }
    }
}

fn main() {
    let mut workers = vec![
        Worker::new("Dana".to_string(), "Google".to_string(), 45),
        Worker::new("Avalonr".to_string(), "Microsoft".to_string(), 71),
        Worker::new("r2d2".to_string(), "Apple".to_string(), 98),
        Worker::new("Albus".to_string(), "OpenAI".to_string(), 67),
    ];
    
    workers.sort();
    
    println!("{:?}", workers);
}
  
  
  
  
/*
run:
  
[Worker { name: "Albus", company: "OpenAI", age: 67 }, Worker { name: "Avalonr", company: "Microsoft", age: 71 }, Worker { name: "Dana", company: "Google", age: 45 }, Worker { name: "r2d2", company: "Apple", age: 98 }]

*/

 





answered Feb 6, 2023 by avibootz

Related questions

1 answer 30 views
1 answer 39 views
1 answer 37 views
1 answer 42 views
...