easy_dev991
struct ComplexModel { let name: String let id: Int let price: Double let isActive: Bool let creationDate: Date let websiteURL: URL? let tags: [String] let statistics: [String: Int] let description: String? } /// Выполнить…
#swift #foundation #keypath #hint
Представляю вам KeyPathComparator, доступный с iOS 15.
Решает задачу из опроса на изи)
Представляю вам KeyPathComparator, доступный с iOS 15.
Решает задачу из опроса на изи)
struct ComplexModel {
let name: String
let id: Int
let price: Double
let isActive: Bool
let creationDate: Date
let websiteURL: URL?
let tags: [String]
let statistics: [String: Int]
let description: String?
}
/// Выполнить работу с массивом и вернуть отсортированный массив
func doWork(with items: [ComplexModel]) -> [ComplexModel] {
// другая логика ...
return items.sorted(using: KeyPathComparator(\.name)) // <- вот
}
/// Выполнить работу с массивом и вернуть отсортированный массив
/// - Parameters:
/// - items: Изначальный массив
/// - order: Порядок сортировки
/// - Returns: Отсортированный массив
func doSortedWork(with items: [ComplexModel], order: SortOrder) -> [ComplexModel] {
// другая логика ...
return items.sorted(using: KeyPathComparator(\.name, order: order))
}
Apple Developer Documentation
KeyPathComparator | Apple Developer Documentation
A comparator that uses another sort comparator to provide the comparison of values at a key path.
👍4🔥2