-
Sorting Using Multiple Criteria ( Swift )Swift/Tips & Tricks 2022. 5. 17. 12:16
self.trialCores = screen.trialCores.sorted { if $0.tag != $1.tag { return $0.tag < $1.tag } else { return $0.direction.count < $1.direction.count } }
let sortedContacts = contacts.sort { if $0.lastName != $1.lastName { // first, compare by last names return $0.lastName < $1.lastName } /* last names are the same, break ties by foo else if $0.foo != $1.foo { return $0.foo < $1.foo } ... repeat for all other fields in the sorting */ else { // All other fields are tied, break ties by last name return $0.firstName < $1.firstName } }
출처: https://stackoverflow.com/questions/37603960/swift-sort-array-of-objects-with-multiple-criteria
'Swift > Tips & Tricks' 카테고리의 다른 글
UIView, Border on specific Sides (Extensions) (0) 2022.06.03 Lottie (0) 2022.05.27 Code Snippet (Xcode) (0) 2022.05.13 Swift Adding/Removing ChildViewController (0) 2022.05.03 Date Format (0) 2022.04.15