如何使用根据 json 中的其他信息计算出的信息创建新列表?

How do I create a new list with information calculated from other information in my json?

#这是我的视图模型#

import Foundation

class CategoriesModel:ObservableObject {
    
    @Published var restaurantList = [Categories]()
    
    init() {
        //get path to json data
        let pathString = Bundle.main.path(forResource: "data", ofType: "json")
        
        if let path = pathString {
            
            // create url object
            let url = URL(fileURLWithPath: path)
            
            do {
                //create data object with data at url
                let data = try Data(contentsOf: url)
                
                let decoder = JSONDecoder()
                
                do {
                    let restaurantData = try decoder.decode([Categories].self, from: data)
                    
                    self.restaurantList = restaurantData
                    //restaurantList.shuffle()
                
                }
                catch {
                    //error decoding json
                    print(error)
                }
                
            }
            catch {
                //error fetching url
                print(error)
            }
            
            
        }
        
        
    }
}

#这是我的模型#

import Foundation

struct Categories: Identifiable, Decodable {
    
    var id = 0
    var name:String = ""
    var truckOrRestaurant = ""
    var type:[String] = [String]()
    var pic:String = ""
    var price:String = ""
    var hours:[String:[String]] = [String:[String]]()
    var hoursString:String = ""
    var stars:String = ""
    var website:String = ""
    var location:String = ""
    var coordinates:[Double] = [Double]()
    var phoneNumber:String = ""
    var currency:String = ""
    var review:String = ""
    var wait:String = ""
    
}

#我的一些观点#

import SwiftUI
import MapKit

struct DiscoverTab: View {
    
    @EnvironmentObject var restaurantInfo:CategoriesModel
    @StateObject private var location = LocationCode()
    
    var body: some View {
        
        
        VStack {
            HStack {
                Text("Sort by:")
                Button(action:{
                    //sort alphabetically
                    //restaurantInfo.restaurantList.sort()
                }, label:{
                    Text("Alphabetical")
                        .background(Color.yellow)
                        .foregroundColor(.black)
                        .cornerRadius(5)
                        
                })
                Button(action:{
                    //sort alphabetically
                    //restaurantInfo.restaurantList.sort()
                }, label:{
                    Text("Distance")
                        .background(Color.yellow)
                        .foregroundColor(.black)
                        .cornerRadius(5)
                })
...

我相信我必须在模型中使用某种不同的属性,但我如何按字母顺序排序,(.sort() 不起作用),以及在我的 json.另外,我将如何对不是来自 json 的数据进行排序?我的视图模型中有一个不同的功能,如果餐厅在任何给定时间开放,则 returns “开放”,反之亦然。每当我尝试在我的视图模型中创建一个新的已发布列表并将餐厅名称附加到它时,我都会收到一个错误。我需要使用一些额外的协议吗?

推荐的方式是对视图模型中的数据进行排序。

首先以单数形式命名模型Category,一个实例是一个类别

struct Category: Identifiable, Decodable {

CategoriesModel中添加一个方法来按键路径对数组进行排序

func sort<T : Comparable>(keyPath: KeyPath<Category,T>) {
    restaurantList.sort { c1, c2 in
        c1[keyPath: keyPath] < c2[keyPath: keyPath]
    }
}

然后,在按钮操作中调用自定义排序方法

Button(action:{
    //sort alphabetically
    restaurantInfo.restaurantList.sort(keyPath: \.name)
}

对于距离,您还必须在视图模型中添加逻辑