-3

I'm using this code in the Playground as an example:

let videosUnsorted = ["C Video", "G Video", "L Video", "T Video", "S Video ", "P Video", "A Video", "Z Video", "R Video"]

let videosValues = [1, 2, 3, 4, 5, 6, 7, 8, 9]

let sorted = videosUnsorted.sorted(by: {$0.videosValues > $1.videosValues})
print(sorted)

but it's giving me this error:

Argument passed to call that takes no arguments

I tried the following but it's not what I'm trying to accomplish. It's only sorting by the elements in the same array:

let sorted = videosUnsorted.sorted(by: {$0.1 < $1.2})
3
  • $0 is a String, and it doesn't have a property videosValues. Commented Nov 14, 2018 at 22:58
  • 1
    See this question Commented Nov 14, 2018 at 22:59
  • 1
    What result do you expect after the sort is complete? Please edit your question (no comments) with the exact results you want. Commented Nov 14, 2018 at 23:02

1 Answer 1

1

The easiest way is to use zip() like this:

zip(videosUnsorted, videosValues).sorted(by: { $0.1 < $1.1 }).map { $0.0 }

This combines the 2 arrays into the type [(String, Int)] you then sort by the Int values and use map() to get back out just the sorted [String].

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.