UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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 Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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.