How to sort Firebase (Firestore) query results in SwiftUI?

Hey There.

Any idea how to sort this in SwiftUI?

//
//  ToDoListItemsView.swift
//  ToDoList
//
//  Created by Neo on 9/14/23.
//

import FirebaseFirestoreSwift
import SwiftUI

struct ToDoListView: View {
    
    @StateObject var viewModel = ToDoListViewViewModel()
    
    @FirestoreQuery var items: [ToDoListItem]
   
    private let userId: String
    
    init(userId: String) {
        self.userId = userId
        self._items = FirestoreQuery(
            collectionPath: "users/\(self.userId)/todos"
        )
     

        
    }

    var body: some View {
        NavigationView{
            VStack {
                List(items) { item in
                    ToDoListItemView(item:item)
                        .swipeActions {
                            Button {
                                viewModel.delete(id: item.id)
                            } label: {
                                
                                Text("Delete")
                                    /// fix this later
                                   
                            }
                            .tint(Color.red)
                            
                        }
                }
                .listStyle(PlainListStyle())
            }
            .navigationTitle("To Do List")
            .toolbar {
                Button {
                    viewModel.showingNewItemView = true
                } label: {
                    Image(systemName:"plus")
                }
            }
            .sheet(isPresented: $viewModel.showingNewItemView){
                NewItemView(newItemPresented: $viewModel.showingNewItemView)
            }
        }
    }
}

struct ToDoListView_Previews: PreviewProvider {
    static var previews: some View {
        ToDoListView(userId: "blahblahblahblahblah")
    }
}

Here is the model:

//
//  ToDoListItem.swift
//  ToDoList
//
//  Created by Neo on 9/14/23.
//

import Foundation

struct ToDoListItem: Codable, Identifiable {
    let id: String
    let title: String
    let dueDate: TimeInterval
    // Add Priority Later
    let createdDate: TimeInterval
    var isDone: Bool
    
    mutating func setDone(_ state: Bool){
        isDone = state
    }
}

I've googled for two hours and cannot sort the final list by "dueDate"....

Posted the same exact question in the Apple Developer Forum and, so far, crickets:

https://developer.apple.com/forums/thread/737760

In most languages, sorting is trivial, so I'm thinking that I'll not be using Firebase whenever possible and stick to SQL databases where sorting is so simple.

:slight_smile:

The good news is that I did manage to create and install my first iOS app and it "works" but needs a lot of refinements.

My First iOS App

FYI:

If anyone wants to learn to build an iOS app:

Tutorial Masterclass Here:

1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.