Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::cmp::{Ordering};
- #[derive(PartialEq, Eq, Debug)]
- struct Thing {
- name: String,
- score: u8,
- }
- impl PartialOrd for Thing {
- fn partial_cmp(&self, other: &Thing) -> Option<Ordering> {
- match self.score.cmp(&other.score) {
- Ordering::Greater => Some(Ordering::Less),
- Ordering::Less => Some(Ordering::Greater),
- Ordering::Equal => Some(self.name.cmp(&other.name)),
- }
- }
- }
- impl Ord for Thing {
- fn cmp(&self, other: &Thing) -> Ordering {
- match self.score.cmp(&other.score) {
- Ordering::Greater => Ordering::Less,
- Ordering::Less => Ordering::Greater,
- Ordering::Equal => self.name.cmp(&other.name),
- }
- }
- }
- impl Thing {
- fn new(name: String, score: u8) -> Self {
- Thing { name, score }
- }
- }
- fn main() {
- let names = vec!["e", "d", "c", "b", "a"];
- let scores = vec![4, 5, 5, 3, 7];
- let mut things = names
- .into_iter()
- .zip(scores.into_iter())
- .map(|(n, s)| Thing::new(n.to_owned(), s))
- .collect::<Vec<_>>();
- things.sort();
- for thing in things {
- println!("{:?}", thing);
- }
- }
Add Comment
Please, Sign In to add comment