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,222 questions

56,124 answers

573 users

How to sort a slice of structs that contain nested structs using custom comparison logic in Go

1 Answer

0 votes
package main
 
import (
    "fmt"
    "sort"
)
 
// ------------------------------------------------------------
// Example: Sorting a slice of user-defined structs
// that contain nested structs
// ------------------------------------------------------------
 
// A nested struct representing an address
type Address struct {
    City string
    Zip  int
}
 
// A struct representing a person, containing a nested Address
type Person struct {
    Name    string
    Age     int
    Address Address
}
 
// ------------------------------------------------------------
// Helper function to print the list
// ------------------------------------------------------------
func printPersons(people []Person) {
    for _, p := range people {
        fmt.Printf("%s | age: %d | city: %s | zip: %d\n",
            p.Name, p.Age, p.Address.City, p.Address.Zip)
    }
}
 
func main() {
    // Create a sample list of people
    people := []Person{
        {"Alice", 30, Address{"San Francisco", 42100}},
        {"Bob", 40, Address{"Austin", 32000}},
        {"Carol", 35, Address{"New York City", 42000}},
        {"Dave", 25, Address{"Austin", 32000}},
        {"Eve", 28, Address{"New York City", 61000}},
    }
 
    // ------------------------------------------------------------
    // Sort using sort.Slice:
    //   1. Compare by city
    //   2. Then by zip
    //   3. Then by age
    // ------------------------------------------------------------
    sort.Slice(people, func(i, j int) bool {
        a, b := people[i], people[j]
 
        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(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

...