Hello guys, I need some help and hope someone has a solution to my problem.
I have a SearchRecipe model in SwiftData which I want to sort when I fetch it.
Here I sort it by the healthScore variable:
[SortDescriptor<SearchRecipe>(\.healthScore)]
However I cannot find a way to sort it by the first object in my nutrients array. If someone has an idea how can I achieve my goal I will be thankful.
@Model
@Codable
final class SearchRecipe: Codable, Sendable, Sequence, IteratorProtocol {
var searchQuery: String?
let image: String
let imageType: String
let title: String
let healthScore: Int
let readyInMinutes: Int
let nutrition: RecipeNutrition
init(image: String, imageType: String, title: String, healthScore: Int, readyInMinutes: Int, nutrition: RecipeNutrition) {
self.image = image
self.imageType = imageType
self.title = title
self.healthScore = healthScore
self.readyInMinutes = readyInMinutes
self.nutrition = nutrition
}
func next() -> SearchRecipe? {
let recipe = self
return recipe
}
}
@Model
@Codable
final class RecipeNutrition: Codable {
let nutrients: [Nutrient]
init(nutrients: [Nutrient]) {
self.nutrients = nutrients
}
}
@Model
@Codable
final class Nutrient: Codable {
let name: String
let amount: Double
let unit: String
let percentOfDailyNeeds: Double
init(name: String, amount: Double, unit: String, percentOfDailyNeeds: Double) {
self.name = name
self.amount = amount
self.unit = unit
self.percentOfDailyNeeds = percentOfDailyNeeds
}
}