Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ordered map in Swift

Is there any built-in way to create an ordered map in Swift 2? Arrays [T] are sorted by the order that objects are appended to it, but dictionaries [K : V] aren't ordered.

For example

var myArray: [String] = []
myArray.append("val1")
myArray.append("val2")
myArray.append("val3")

//will always print "val1, val2, val3"
print(myArray)


var myDictionary: [String : String] = [:]
myDictionary["key1"] = "val1"
myDictionary["key2"] = "val2"
myDictionary["key3"] = "val3"

//Will print "[key1: val1, key3: val3, key2: val2]"
//instead of "[key1: val1, key2: val2, key3: val3]"
print(myDictionary)

Are there any built-in ways to create an ordered key : value map that is ordered in the same way that an array is, or will I have to create my own class?

I would like to avoid creating my own class if at all possible, because whatever is included by Swift would most likely be more efficient.

like image 478
Jojodmo Avatar asked Jun 21 '15 21:06

Jojodmo


People also ask

Is there an ordered dictionary in Swift?

There is no order. Dictionaries in Swift are an unordered collection type. The order in which the values will be returned cannot be determined. If you need an ordered collection of values, I recommend using an array.

What is mapping in Swift?

Swift version: 5.6. The map() method allows us to transform arrays (and indeed any kind of collection) using a transformation closure we specify. The return value will be an array of the same size, containing your transformed elements. For example, given the following array: let numbers = [1, 2, 3, 4]

What is a Swift dictionary?

Swift dictionary is an unordered collection of items. It stores elements in key/value pairs. Here, keys are unique identifiers that are associated with each value.

How do you sort an array in Swift?

To sort the array we use the sort() function. This function is used to sort the elements of the array in a specified order either in ascending order or in descending order. It uses the “>” operator to sort the array in descending order and the “<” operator to sort the array in ascending order.


4 Answers

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).

like image 162
chrisamanse Avatar answered Sep 18 '22 19:09

chrisamanse


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

var array = [(name: String, value: String)]() // add elements array.sort() { $0.name < $1.name } // or array.sort() { $0.0 < $1.0 } 
like image 37
Mundi Avatar answered Sep 18 '22 19:09

Mundi


"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

like image 24
coffeecoder Avatar answered Sep 20 '22 19:09

coffeecoder


You can use KeyValuePairs, from documentation:

Use a KeyValuePairs instance when you need an ordered collection of key-value pairs and don’t require the fast key lookup that the Dictionary type provides.

let pairs: KeyValuePairs = ["john": 1,"ben": 2,"bob": 3,"hans": 4]
print(pairs.first!)

//prints (key: "john", value: 1)

like image 28
barola_mes Avatar answered Sep 18 '22 19:09

barola_mes