-3

I've seen this has been done before but can't seem to find it in swift. So I have two arrays, arrayA and arrayB. I want arrayB to sort itself into the same order a arrayA. So for example her is the arrays:

var arrayA = [1,2,3,4,5]
var arrayB = [4,5,1,2,3]

Thanks

Henry Brown
  • 2,219
  • 8
  • 31
  • 48
  • You want to sort with increase and with decrease? – cmashinho Apr 25 '15 at 16:07
  • I want arrayB to become arrayA, they have the same elements inside. but arrayB is in the wrong order – Henry Brown Apr 25 '15 at 16:09
  • you need to make sort `arrayB` using simple sorting algorithms such as "Insertion", "Selection", and others. – cmashinho Apr 25 '15 at 16:13
  • And how do I do that? – Henry Brown Apr 25 '15 at 16:14
  • just use this documentation about [sorting algorithms](http://www.sorting-algorithms.com) – cmashinho Apr 25 '15 at 16:15
  • There was a reason I asked the question, so I could find the answer quickly and efficiently. Not to have to learn algorithms. – Henry Brown Apr 25 '15 at 16:17
  • 3
    If you want them to be the same, why not just copy? arrayA = arrayB? – Aaron Rasmussen Apr 25 '15 at 16:18
  • In what sense? This question is obviously a much simpler form of what I want to achieve. – Henry Brown Apr 25 '15 at 16:19
  • @RomanSausarnes, thought the same. But maybe those elements are classes with different values and he wants to sort them based on a certain value? – Eendje Apr 25 '15 at 16:20
  • @HenryBrown: *"This question is obviously a much simpler form of what I want to achieve"* – Then why don't you show a non-trivial example that shows what you want to achieve? – Martin R Apr 25 '15 at 16:24
  • 1
    Your question is still unclear to me, but this *might* be what you are looking for: http://stackoverflow.com/questions/29432656/how-can-i-sort-multiple-arrays-based-on-the-sorted-order-of-another-array. – Martin R Apr 25 '15 at 16:26
  • arrayA and arrayB both have the same elements, I simply want to change the order of arrayB to become in the same order as arrayA. There must be a method or something for doing this?? – Henry Brown Apr 25 '15 at 16:34
  • 2
    @HenryBrown: If they have the same elements then you can simply assign `arrayB = arrayA`. – Martin R Apr 25 '15 at 16:37
  • 2
    This question takes the prize as the most unclear or unnecessary ever asked on Stack Overflow. – matt Apr 25 '15 at 16:56

2 Answers2

3

1) This is what I understand

arrayA defines some kind of order. In your example it is coincidentally the ascending order of integers but it can really be any kind of order like one of the following:

  1. Example 1: [5, 4, 1, 3, 2] Ascending by number "names" (five, four, one, three, two)
  2. Example 2: [5, 2, 4, 3, 1]: Ascending by italian names of numbers (cinque, due, quattro, tre, uno)

2) You don't like this kind of solution

arrayB = arrayA

You actually need a way to move the elements inside arrayB in order to make it sorted as arrayA (maybe because you need a solution for a more generic problem where the elements of the arrays are not simply integers).

3) Now, my solution

var arrayA = [1, 2, 3, 4, 5]
var arrayB = [4, 5, 1, 2, 3]

var indexes = [Int: Int]()
for (index, elm) in enumerate(arrayA)  {
    indexes[elm] = index
}

// now indexes[i] gives me the position of the integer i inside arrayA, e.g. indexes[3] -> 2

arrayB.sort { return indexes[$0] < indexes[$1] }

// now arrayB has been sorted as arrayA

Conclusion

This approach does work when:

  1. there are not duplicates in arrayA (or arrayB)
  2. arrayA.count == arrayB.count
  3. arrayA and arrayB contain the same elements

Please let me know if this is what you are looking for.

Luca Angeletti
  • 58,465
  • 13
  • 121
  • 148
-1

I think you just need to use the reverse() function.

var arrayA = [1,2,3,4,5]

arrayB = reverse(arrayA)

will result in arrayB = [5,4,3,2,1] and vice versa.

Bence Kaulics
  • 7,066
  • 7
  • 33
  • 63