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

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,102 questions

55,976 answers

573 users

How to sort an array of structs that contain nested structs using custom comparison logic in Swift

2 Answers

0 votes
import Foundation
 
// ------------------------------------------------------------
// Example: Sorting an array of user-defined structs
// that contain nested structs
// ------------------------------------------------------------
 
// A nested struct representing an address
struct Address {
    var city: String
    var zip: Int
}
 
// A struct representing a person, containing a nested Address
struct Person {
    var name: String
    var age: Int
    var address: Address
}
 
// ------------------------------------------------------------
// Helper function to print the list
// ------------------------------------------------------------
func printPersons(_ people: [Person]) {
    for p in people {
        print("\(p.name) | age: \(p.age) | city: \(p.address.city) | zip: \(p.address.zip)")
    }
}
 
// ------------------------------------------------------------
// Main program
// ------------------------------------------------------------
let people: [Person] = [
    Person(name: "Alice", age: 30,
           address: Address(city: "San Francisco", zip: 42100)),
    Person(name: "Bob", age: 40,
           address: Address(city: "Austin", zip: 32000)),
    Person(name: "Carol", age: 35,
           address: Address(city: "New York City", zip: 42000)),
    Person(name: "Dave", age: 25,
           address: Address(city: "Austin", zip: 32000)),
    Person(name: "Eve", age: 28,
           address: Address(city: "New York City", zip: 61000))
]
 
// ------------------------------------------------------------
// Sort using sorted(by:):
//   1. Compare by city
//   2. Then by zip
//   3. Then by age
// ------------------------------------------------------------
let sorted = people.sorted { a, b in
    if a.address.city != b.address.city {
        return a.address.city < b.address.city
    }
    if a.address.zip != b.address.zip {
        return a.address.zip < b.address.zip
    }
    return a.age < b.age
}
 
// Print the sorted result
printPersons(sorted)
 
 
/*
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
0 votes
import Foundation

// A nested struct representing an address
struct Address {
    let city: String
    let zip: Int
}

// A struct representing a person, containing a nested Address
struct Person {
    let name: String
    let age: Int
    let address: Address
}

// Print each person in a formatted line
func printPersons(_ people: [Person]) {
    for p in people {
        print("\(p.name) | age: \(p.age) | city: \(p.address.city) | zip: \(p.address.zip)")
    }
}

// Sort persons by city, then zip, then age
func sortPersons(_ people: [Person]) -> [Person] {
    people.sorted {
        if $0.address.city != $1.address.city {
            return $0.address.city < $1.address.city
        }
        if $0.address.zip != $1.address.zip {
            return $0.address.zip < $1.address.zip
        }
        return $0.age < $1.age
    }
}

// Sample data
let people: [Person] = [
    Person(name: "Alice", age: 30, address: Address(city: "San Francisco", zip: 42100)),
    Person(name: "Bob",   age: 40, address: Address(city: "Austin",        zip: 32000)),
    Person(name: "Carol", age: 35, address: Address(city: "New York City", zip: 42000)),
    Person(name: "Dave",  age: 25, address: Address(city: "Austin",        zip: 32000)),
    Person(name: "Eve",   age: 28, address: Address(city: "New York City", zip: 61000))
]

// Perform sorting
let sorted = sortPersons(people)

// Display sorted results
printPersons(sorted)


/*
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 2 by avibootz

Related questions

...