-2

I have three arrays like ["a","d","c"] ["x,p,z"] ["o,n,q"]. I want to merge an array to array into single array like this ["a","c","d","n","o","p","q","x","z"] and have to sort it.

I tried like this way

var arr1 = ["a","x","d"]
let str2 = "byq".characters.map { String($0) } // Manually i did string to array conversion
var arr2 = Array(str2)
let str3 = "cmo".characters.map { String($0) } // Manually i did string to array conversion
var arr3 = Array(str3)

var formattedArr2: [String] = [(arr2.map{String($0)}).joined(separator: ",")]

func combineArrays<T>(arrays:[[T]]) -> [T] {
    let maxCount = arrays.reduce(0) {max($0, $1.count)}
    var result = [T]()

    for i in 0..<maxCount {
        for array in arrays {
            if i < array.count {
                result.append(array[i])
            }
        }
    }
    return result
}

print(combineArrays(arrays: [arr1,arr2,arr3]).sorted())

And I got the answer as ["a","c","d","n","o","p","q","x","z"]. But i did it in wrong way. Please anyone can give ideas to merge an array to array in single array

how to merge this array and split into single characters from [["a","d","c"],["x,p,z"],["o,n,q"]]?

Muthu Sabarinathan
  • 1,198
  • 2
  • 21
  • 49
  • *"And i got the answer as [...] what i expect is i got it"* - did you get the correct result or not? In the first case what is the purpose of the question, in the latter what is the expected output? – luk2302 Oct 23 '17 at 19:09
  • The output is correct, but I believe he is asking for a succinct and efficient way to merging and sorting arrays. – user3483203 Oct 23 '17 at 19:12
  • @chris - Yes chris. what i did, i convert string into array and merge it. But i want to convert array to array to single array – Muthu Sabarinathan Oct 23 '17 at 19:14
  • @vadian - Why you mentioned this question as duplicate? If duplicate means can you able to solve this using that original question????? – Muthu Sabarinathan Oct 23 '17 at 20:24
  • The duplicate shows you the way to *merge an array to array in single array*. OK, the sorting is not mentioned but you got the proper sorting in your question. – vadian Oct 23 '17 at 20:26
  • @vadian - Please check the question title. I used string array but on that question they used number. And I tried that question too. It is not working for string. – Muthu Sabarinathan Oct 23 '17 at 20:28
  • All three suggested ways (`flatMap`, `reduce` and `joined()`) work also with strings. I tested it with your code to create the 3 arrays. I edited and undeleted my answer. – vadian Oct 23 '17 at 20:34

1 Answers1

4

Are you looking for something like this?

let sortedArray = [arr1, arr2, arr3].flatMap { $0 }.sorted()

Edit: Or maybe this?

let sortedArray = ([["a","d","c"],["x,p,z"],["o,n,q"]].flatMap { $0 } as [String]).flatMap { $0.split(separator: ",")}.sorted()

Edit (same result, but preferred by poster):

let sortedArray = ([["a","d","c"],["x,p,z"],["o,n,q"]].flatMap { $0 } as [String]).flatMap { $0.components(separatedBy: ",") }.sorted()
Matusalem Marques
  • 2,399
  • 2
  • 18
  • 28