Hi my problem is that i want to sort an array of objects by the object's title property. When i change the title's type from String to LocalizedStringKey i get an error. Is there a way to sort the correct translated string behind the localizedStringKey.
struct Item: Codable,Comparable, Hashable, Identifiable {
static func < (lhs: Item, rhs: Item) -> Bool {
return lhs.title < rhs.title
}
var id: Int
let image: String
let color: String
// title should be LocalizedStringKey
let title: String
}
......
@State private var sortedDown = false
var filteredItems: [Item] {
var sortedItems: [Item]
let filteredItems = modelData.items.filter { item in
(!showFavoritesOnly || item.isFavorite)
}
if sortedDown {
sortedItems = filteredItems.sorted(by: { (item1, item2) -> Bool in
return item1.title > item2.title
})
} else {
sortedItems = filteredItems.sorted(by: { (item1, item2) -> Bool in
return item1.title < item2.title
})
}
return sortedItems
}
var body: some View {
NavigationView {
List {
Toggle(isOn: $showFavoritesOnly, label: {
Text("showFavorites")
})
ForEach(filteredItems) { (item) in
.....
``