Hi, I have an array of Strings and want to sort it with Sections from a to z in a list. How can I do it?
@Konste4sfi
A simplified example:
You may need to modify many parts of this code, depending on the details of your requirement.
You need to create a structure which represents sections, and rows in each section.want to sort it with Sections from a to z in a list
A simplified example:
Code Block import SwiftUI struct ContentView: View { var inputArray: [String] = [ "Andrew", "Adam", "David" ] @State var groupedArray: [String: [String]] = [:] var body: some View { List { ForEach(groupedArray.keys.sorted(), id: \.self) {key in Section(header: Text(key)) { ForEach(groupedArray[key]!, id: \.self) {value in Text(value) } } } } .onAppear { groupedArray = Dictionary( grouping: inputArray, by: {$0.first?.uppercased() ?? ""} ).mapValues{$0.sorted()} } } }
You may need to modify many parts of this code, depending on the details of your requirement.