Ordered map in Swift Ordered map in Swift ios ios

Ordered map in Swift


You can order them by having keys with type Int.

var myDictionary: [Int: [String: String]]?

or

var myDictionary: [Int: (String, String)]?

I recommend the first one since it is a more common format (JSON for example).


Just use an array of tuples instead. Sort by whatever you like. All "built-in".

var array = [(name: String, value: String)]()// add elementsarray.sort() { $0.name < $1.name }// orarray.sort() { $0.0 < $1.0 }


"If you need an ordered collection of key-value pairs and don’t need the fast key lookup that Dictionary provides, see the DictionaryLiteral type for an alternative." - https://developer.apple.com/reference/swift/dictionary