-1

I am trying to make a list with a dictionary. My dictionary is located in a model. My dictionary is [String : String]. I tried to sort it hopefully it is sorted by alphabetical. I couldn't figure it out why it does not work

var fw:  Deck

 var body: some View {
                
        let sortedDict  = fw.dictItems.sorted(by:  < )
        let keys = sortedDict.map {$0.key}
        let values = sortedDict.map {$0.value}
  return List{

            ForEach(keys.indices) { index in
                    HStack {
                        Text(keys[index])
                        Text("\(values[index])")
                    }
                }
}
 }
        
            }
onqun
  • 13
  • 4

1 Answers1

0

Here is a way for you:

struct ContentView: View {
    
    @State var entries: [String: String] = ["key1":"val1", "key2":"val2", "key3":"val3", "key4":"val4"]
    
    var body: some View {
        List {
            ForEach(entries.keys.sorted(by: <), id: \.self) { key in
                HStack {
                    Text(key)
                    
                    Text(entries[key]!)
                }
            }
        }
    }
}
ios coder
  • 1
  • 4
  • 31
  • 91