對“全名”的可選字串陣列進行排序的最佳方法是什么
更新:很抱歉..我忘記了最重要的一點。需要按姓氏、名字順序和單名排序,nil 排在最前面
let unordered:[String?] = [
"Zach Appletree",
"Nancy Crabtree",
"Bill",
nil,
"Bonny Appletree",
"Zach Johnson",
"Paul Brandon"
]
let sortedArray = unordered.sorted(by: { } )
uj5u.com熱心網友回復:
PersonNameComponents不是,Comparable除非您自己定義一致性。
例如
unordered
.lazy
.compactMap { $0.flatMap(PersonNameComponentsFormatter().personNameComponents) }
.sorted()
extension PersonNameComponents: Comparable {
public static func < (components0: Self, components1: Self) -> Bool {
var fallback: Bool {
[\PersonNameComponents.givenName, \.middleName].contains {
Optional(components0[keyPath: $0], components1[keyPath: $0])
.map { $0.lowercased().isLessThan($1.lowercased(), whenEqual: false) }
?? false
}
}
switch (
components0.givenName?.lowercased(), components0.familyName?.lowercased(),
components1.givenName?.lowercased(), components1.familyName?.lowercased()
) {
case let (
_, familyName0?,
_, familyName1?
):
return familyName0.isLessThan(familyName1, whenEqual: fallback)
case (
_, let familyName0?,
let givenName1?, nil
):
return familyName0.isLessThan(givenName1, whenEqual: fallback)
case (
let givenName0?, nil,
_, let familyName1?
):
return givenName0.isLessThan(familyName1, whenEqual: fallback)
default:
return fallback
}
}
}
public extension Comparable {
/// Like `<`, but with a default for the case when `==` evaluates to `true`.
func isLessThan(
_ comparable: Self,
whenEqual default: @autoclosure () -> Bool
) -> Bool {
self == comparable
? `default`()
: self < comparable
}
}
public extension Optional {
/// Exchange two optionals for a single optional tuple.
/// - Returns: `nil` if either tuple element is `nil`.
init<Wrapped0, Wrapped1>(_ optional0: Wrapped0?, _ optional1: Wrapped1?)
where Wrapped == (Wrapped0, Wrapped1) {
self = .init((optional0, optional1))
}
/// Exchange two optionals for a single optional tuple.
/// - Returns: `nil` if either tuple element is `nil`.
init<Wrapped0, Wrapped1>(_ optionals: (Wrapped0?, Wrapped1?))
where Wrapped == (Wrapped0, Wrapped1) {
switch optionals {
case let (wrapped0?, wrapped1?):
self = (wrapped0, wrapped1)
default:
self = nil
}
}
}
uj5u.com熱心網友回復:
使用 compactMap(_:)
https://developer.apple.com/documentation/swift/sequence/2950916-compactmap
let unordered:[String?] = [
"Zach Appletree",
"Nancy Crabtree",
"Bill",
nil,
"Bonny Appletree",
"Zach Johnson",
"Paul Brandon"
]
let sortedArray = unordered.compactMap { $0 }.sorted()
print(sortedArray)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/354284.html
上一篇:如何從串列中重建影像物件?
