TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Day 42 - Alphabetical Astronaut List

Forums > 100 Days of SwiftUI

@SD449  

I am following Day 42 of Hacking with Swift and trying to sort my astronauts list alphabetically. But it doesn't work because my Astronaut is a custom type.

I have tried added .sorted() but it doesn't seem to be working.

var body: some View {
    NavigationView {
        if astronautsView {
            List(astronauts.sorted()) { astronaut in //here it crashes and dont sort
                NavigationLink(destination: AstronautView(astronaut: astronaut)) {
                    Image(astronaut.id)
                        .resizable()
                        .scaledToFill()
                        .frame(width: 44, height: 44)
                    .clipShape(Circle())
                        .shadow(radius: 10)
                    VStack(alignment: .leading) {
                        Text(astronaut.id.capitalizingFirstLetter())
                            .font(.headline)
                        Text(astronaut.name)
                    }
                }
                .navigationBarTitle("Moonshot")
                .navigationBarItems(trailing:
                    Button(action: {
                        self.astronautsView = !self.astronautsView }, label: { Text(self.astronautsView ? "Missions" : "Astronauts")
                    })
                )
            }

Thank you for reading and helping

3      

@SD449  

I worked it out!!

Needed to add Comparable to my Struct and the set the method for sorting.

Very proud of myself and has given me the bump I needed to push on with the course as I was getting dispondent and questioning my inteligence/ability.

3      

congratulations for sorting it out yourself! I've done that project as well, it was hell of a challenge. Dop you mind elaborating what you mean by customizing the type for Astronaut?

3      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.