-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"]]?

Sabs
  • 1,168
  • 2
  • 19
  • 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 – Sabs 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????? – Sabs 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. – Sabs 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,359
  • 2
  • 18
  • 28
  • Thanks for quick reply. It will work like ["a","c","d","o,n,q","x,p,z"]. But what i expect is ["a","c","d","n","o","p","q","x","z"] – Sabs Oct 23 '17 at 19:19
  • let adc = [ "a", "d", "c" ] let xpz = [ "x", "p", "z" ] let onq = [ "o", "n", "q" ] var val = adc + xpz + onq val = val.sorted() This should work. – ShivamD Oct 23 '17 at 20:06
  • @Sabs Did you see my edit? Was that closer to what you were looking for? – Matusalem Marques Oct 24 '17 at 09:58
  • @MatusalemMarques No matusalemMarques. I got the output as ["a","c","d","o,n,q","x,p,z"]. But my expected output is ["a","c","d","n","o","p","q","x","z"] – Sabs Oct 24 '17 at 12:11
  • @Sabs Trying the second example? Are you sure? I get the exact output you want. Excepto for the sorting which I forgot but was trivial to add. – Matusalem Marques Oct 24 '17 at 12:14
  • @MatusalemMarques - I will try this and let you know. – Sabs Oct 24 '17 at 12:18
  • Thanks @MatusalemMarques. It is working but i changed split into component separated by let sortedArray = ([["a","d","c"],["x,p,z"],["o,n,q"]].flatMap { $0 } as [String]).flatMap { $0.components(separatedBy: ",")}.sorted() – Sabs Oct 24 '17 at 12:22
  • Please edit your answer. split to component and separator to separatedBy. I will mention this as correct answer. – Sabs Oct 24 '17 at 12:23