Pick the fix
Sorting with a key path in place of the comparator doesn't compile. Which works with only the standard library?
Hint 1 of 1
What does sorted(by:) expect?
Choose an answer, then open Show answer below to check it.
Show answer
Answer: A people.sorted { $0.age < $1.age }
Correct. sorted(by:) needs a comparison closure.
Why not the others?
- B
people.sorted(\.age). No such overload exists in the standard library. - C
people.sorted(by: \Person.age). A key path to Int isn't a(Person, Person) -> Boolcomparator. - D
people.sorted(using: KeyPathComparator(\.age)). This works, but it comes from Foundation (iOS 15+), not the standard library.