-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

asked Apr 25, 2015 at 15:50
14
  • You want to sort with increase and with decrease? Commented Apr 25, 2015 at 16:07
  • 3
    If you want them to be the same, why not just copy? arrayA = arrayB? Commented Apr 25, 2015 at 16:18
  • 1
    Your question is still unclear to me, but this might be what you are looking for: stackoverflow.com/questions/29432656/…. Commented Apr 25, 2015 at 16:26
  • 2
    @HenryBrown: If they have the same elements then you can simply assign arrayB = arrayA. Commented Apr 25, 2015 at 16:37
  • 2
    This question takes the prize as the most unclear or unnecessary ever asked on Stack Overflow. Commented Apr 25, 2015 at 16:56

2 Answers 2

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.

answered Apr 25, 2015 at 17:15
1
  • Yes! Perfect! Thank you exactly what I wanted! Commented Apr 25, 2015 at 17:17
-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,2977 gold badges38 silver badges69 bronze badges
answered Apr 25, 2015 at 16:31

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.