I have a core data:
entity: Songs
attributes: singer, song
If I want to GROUP & SORT the fetched core data, and make a List---(Section: Singer), (Item: Song), how can I do it?
import SwiftUI
import CoreData
struct SongList: View {
@FetchRequest(entity: Songs.entity(), sortDescriptors: [NSSortDescriptor(keyPath: \Songs.song, ascending: true)]) var tutorial: FetchedResults<Tutorial>
func group(_ result : FetchedResults<Songs>)-> [[Songs]] {
return Dictionary(grouping: result) { (element : Songs) in
element.singer!.sorted()
}
.values.map{$0}
}
Using the function, the fetched core data ("singer"), can be grouped and use in the Section of my List. However, the main probem is the order is arbitrary every time, as Dictionary is "unordered". How can I make it in order, if keep using Dictionary? Or, is there any other solution?
The fetched core data ("song"), is in order, in the Item of my List.