Combining two arrays and sorting the arrays Swift -


i looking along lines of combining 2 arrays of different types. trying this

var alphabet = [b,c,a,e,f,d] var numbers = [2,3,1,5,6,4] 

as can see, numbers , letters correspond each other in list above, out of place.

i spit out letters , numbers in order such as

[(a,1)(b,2)(c,3)(d,4)(e,5)(f,6)] 

i appreciate help, thank you

  • zip 2 arrays, gives array of tuples (pairs) [(b, 2), (c, 3), ... ].
  • sort array of tuples respect first component:

swift 2 code:

let result = zip(alphabet, numbers).sort { $0.0 < $1.0 } print(result) // [("a", 1), ("b", 2), ("c", 3), ("d", 4), ("e", 5), ("f", 6)] 

for swift 3, replace sort sorted.


Comments