Hi I do have array of custom objects in swift, like below
Objects of below class
Class Person {
let name: String
let pointsEarned: CGFloat
}
Array is like below
let person1 = Person(“name1”, “5.6”)
let person2 = Person(“name2”, “6.6”)
let person3 = Person(“name3”, “1.6”)
let persons = [person1, person2, person3 ]
I would like find person who’s earned points are close to 7.0
Is there extension on array I can write for this?
Appreciate any help! Thanks.
abs(7 - pointsEarned), so sort the objects by whichever has the lowest distencepeople.sort { abs(7 - $0.score) < abs(7 - $1.score) }sort, and then it'speople.sort{ insideClosure }.firstwhile it could just bepeople.min(by: { insideClosure }