-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})
rmaddy
  • 314,917
  • 42
  • 532
  • 579

1 Answers1

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].

bscothern
  • 1,894
  • 11
  • 15