How to sort a vector of structs that contain nested structs using custom comparison logic in Rust

1 Answer

0 votes
use std::cmp::Ordering;
 
// ------------------------------------------------------------
// Example: Sorting a vector of user-defined structs
// that contain nested structs
// ------------------------------------------------------------
 
// A nested struct representing an address
#[derive(Debug)]
struct Address {
    city: String,
    zip: i32,
}
 
// A struct representing a person, containing a nested Address
#[derive(Debug)]
struct Person {
    name: String,
    age: i32,
    address: Address,
}
 
// ------------------------------------------------------------
// Helper function to print the list
// ------------------------------------------------------------
fn print_persons(people: &[Person]) {
    for p in people {
        println!(
            "{} | age: {} | city: {} | zip: {}",
            p.name, p.age, p.address.city, p.address.zip
        );
    }
}
 
fn main() {
    // Create a sample list of people
    let mut people = vec![
        Person {
            name: "Alice".into(),
            age: 30,
            address: Address {
                city: "San Francisco".into(),
                zip: 42100,
            },
        },
        Person {
            name: "Bob".into(),
            age: 40,
            address: Address {
                city: "Austin".into(),
                zip: 32000,
            },
        },
        Person {
            name: "Carol".into(),
            age: 35,
            address: Address {
                city: "New York City".into(),
                zip: 42000,
            },
        },
        Person {
            name: "Dave".into(),
            age: 25,
            address: Address {
                city: "Austin".into(),
                zip: 32000,
            },
        },
        Person {
            name: "Eve".into(),
            age: 28,
            address: Address {
                city: "New York City".into(),
                zip: 61000,
            },
        },
    ];
 
    // ------------------------------------------------------------
    // Sort using sort_by:
    //   1. Compare by city
    //   2. Then by zip
    //   3. Then by age
    // Mirrors the C++ comparator logic cleanly.
    // ------------------------------------------------------------
    people.sort_by(|a, b| {
        match a.address.city.cmp(&b.address.city) {
            Ordering::Equal => match a.address.zip.cmp(&b.address.zip) {
                Ordering::Equal => a.age.cmp(&b.age),
                other => other,
            },
            other => other,
        }
    });
 
    // Print the sorted result
    print_persons(&people);
}
 
 
/*
run:
 
Dave | age: 25 | city: Austin | zip: 32000
Bob | age: 40 | city: Austin | zip: 32000
Carol | age: 35 | city: New York City | zip: 42000
Eve | age: 28 | city: New York City | zip: 61000
Alice | age: 30 | city: San Francisco | zip: 42100
 
*/

 



answered Sep 1 by avibootz
edited Sep 1 by avibootz

Related questions

...