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

SOLVED: How do I make enum values work for sorting

Forums > SwiftUI

I'd like to sort a list that holds the data that you input and as you can see the code below I'm using enum and looping it over for the Picker View so that you can choose the options to sort the list as how you want. However, even if you chose the sorting options from the picker, the list doesn't change anyting. I'd appreciate any help.

    struct PastSpent: View {

        enum SortType: String, CaseIterable {
            case date
            case expensive
            case cheap
        }
        @State var sort: SortType = .cheap

        @Environment(\.managedObjectContext) var moc
        @FetchRequest(sortDescriptors: []) var finances: FetchedResults<Finance>
        @EnvironmentObject var chosenColor: ColorTheme // Get the object from the environment
        var addspent = AddSpent()
        var currencySetting = CurrencySetting()
        let ordering: SortType

        var body: some View {
            NavigationView {
                VStack {
                    Picker("sort by...", selection: $sort) {
                        ForEach(SortType.allCases, id: \.self) { item in
                            Text(item.rawValue)
                        }
                    }
                    .padding()
                    .background(chosenColor.cc.opacity(0.3))
                    .cornerRadius(10)
                    .padding()
                    .pickerStyle(.segmented)
                    List {
                        ForEach(sortingOptions) { finance in
                            HStack {
                                VStack(alignment: .leading) {
                                    Text(finance.name ?? "")
                                        .foregroundColor(chosenColor.cc)
                                    Text(finance.category ?? "")
                                        .foregroundColor(.secondary)
                                }
                                Spacer()
                                VStack {
                                    Text("\(finance.spending, format: .currency(code: addspent.currency))")
                                        .font(.headline)

                                    Text((finance.date?.displayFormat)!)
                                        .foregroundColor(.secondary)
                                }
                            }
                        }
                        .onDelete(perform: deleteSpending)
                    }
                    .navigationTitle("Past Spendings")
                    .navigationBarTitleDisplayMode(.inline)

                    .toolbar {
                        ToolbarItem(placement: .navigationBarLeading) {
                            EditButton()
                                .foregroundColor(chosenColor.cc)
                        }
                    }
                }
            }
        }

        func deleteSpending(at offsets: IndexSet) {
            for offset in offsets {
                let finance = finances[offset]
                moc.delete(finance)
            }
            try? moc.save()
        }

        var sortingOptions: [Finance] {
            switch ordering {
            case .date:
                return finances.sorted { $0.date! < $1.date! }
            case .expensive:
                return finances.sorted { $0.spending > $1.spending }
            case .cheap:
                return finances.sorted { $0.spending < $1.spending }
            }
        }
    }

        extension Date {
            var displayFormat: String {
                self.formatted(
                   .dateTime.month().day().weekday()
                )
            }
        }

    struct PastSpent_Previews: PreviewProvider {
        static var previews: some View {
            PastSpent(ordering: .expensive)
        }
    }

2      

var sortingOptions: [Finance] {
    switch ordering {
    case .date:
        return finances.sorted { $0.date! < $1.date! }
    case .expensive:
        return finances.sorted { $0.spending > $1.spending }
    case .cheap:
        return finances.sorted { $0.spending < $1.spending }
    }
}

You need to be switching on sort rather than ordering, as it's sort that you are setting in your Picker.

3      

@roosterboy Thank you so much for your answer it worked now ;) I didn't have the idea of switching on that ^^;

I appreciate your help!

2      

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.