I was trying to group an array. For convenience I have used an example from the documentation.
Question:
- Is the sequence returned for each letter ordered guaranteed to be in the same order as the input ?
Note:
- Based on my testing it seems to be ordered, just wanted to check if it was guaranteed to be ordered as the input.
- Notice the order of
Kofi
andKweku
are interchanged in example 2. - Using Swift 4.2, Xcode: Version 10.1 (10B61)
Example 1:
let students = ["Kofi", "Abena", "Efua", "Kweku", "Akosua"]
let studentsByLetter = Dictionary(grouping: students, by: { $0.first! })
print(studentsByLetter)
["K": ["Kofi", "Kweku"], "A": ["Abena", "Akosua"], "E": ["Efua"]]
Example 2:
let students = ["Kweku", "Abena", "Efua", "Kofi", "Akosua"]
let studentsByLetter = Dictionary(grouping: students, by: { $0.first! })
print(studentsByLetter)
["K": ["Kweku", "Kofi"], "A": ["Abena", "Akosua"], "E": ["Efua"]]