文章40 | 阅读 18235 | 点赞0
let numbers = [3, 5, 6, 1, 8, 2]
let sortedNumbers = numbers.sorted { (lhs, rhs) in
return lhs < rhs
}
// [1, 2, 3, 5, 6, 8]
struct BlogPost {
let title: String
let pageView: Int
let sessionDuration: Double
}
extension BlogPost {
static var examples: [BlogPost] = [
BlogPost(title: "Alice", pageView: 1, sessionDuration: 3),
BlogPost(title: "Peter", pageView: 1, sessionDuration: 2),
BlogPost(title: "Kofi", pageView: 1, sessionDuration: 1),
BlogPost(title: "Akosua", pageView: 5, sessionDuration: 2),
BlogPost(title: "Abena", pageView: 4, sessionDuration: 10)
]
}
let sortedObjects = objects.sorted { (lhs, rhs) in
for (lhsCriteria, rhsCriteria) in [(lhsCrtria1, rhsCriteria1), (lhsCrtria2, rhsCriteria2), (lhsCrtria3, rhsCriteria3), ... , (lhsCrtriaN, rhsCriteriaN)] { // ①
if lhsCriteria == rhsCriteria { // ②
continue
}
return lhsCriteria < rhsCriteria // ③
}
}
struct BlogPost {
let title: String
let pageView: Int
let sessionDuration: Double
}
extension BlogPost {
static var examples: [BlogPost] = [
BlogPost(title: "Alice", pageView: 1, sessionDuration: 3),
BlogPost(title: "Peter", pageView: 1, sessionDuration: 2),
BlogPost(title: "Kofi", pageView: 1, sessionDuration: 1),
BlogPost(title: "Akosua", pageView: 5, sessionDuration: 2),
BlogPost(title: "Abena", pageView: 4, sessionDuration: 10)
]
}
let popularPosts = BlogPost.examples.sorted { (lhs, rhs) in if lhs.pageView == rhs.pageView { // ① return lhs.sessionDuration > rhs.sessionDuration }
return lhs.pageView > rhs.pageView // ②
}
[BlogPost(title: "Akosua", pageView: 5, sessionDuration: 2.0),
BlogPost(title: "Abena", pageView: 4, sessionDuration: 10.0),
BlogPost(title: "Alice", pageView: 1, sessionDuration: 3.0),
BlogPost(title: "Peter", pageView: 1, sessionDuration: 2.0),
BlogPost(title: "Kofi", pageView: 1, sessionDuration: 1.0)]
extension BlogPost {
static var examples2: [BlogPost] = [
BlogPost(title: "Zoo", pageView: 5, sessionDuration: 2),
BlogPost(title: "Alice", pageView: 1, sessionDuration: 3),
BlogPost(title: "Peter", pageView: 1, sessionDuration: 2),
BlogPost(title: "Kofi", pageView: 1, sessionDuration: 1),
BlogPost(title: "Akosua", pageView: 5, sessionDuration: 2),
BlogPost(title: "Abena", pageView: 4, sessionDuration: 10),
BlogPost(title: "Angero", pageView: 1, sessionDuration: 2)
]
}
let popularPosts = BlogPost.examples2.sorted { (lhs, rhs) in
if lhs.pageView == rhs.pageView {
// 添加另一个if来检查博客文章是否具有相同的会话持续时间,如果它们具有相同的页面浏览次数和会话持续时间,则按标题对它们进行排序
if lhs.sessionDuration == rhs.sessionDuration {
return lhs.title < rhs.title
}
return lhs.sessionDuration > rhs.sessionDuration
}
return lhs.pageView > rhs.pageView
}
[BlogPost(title: "Akosua", pageView: 5, sessionDuration: 2.0),
BlogPost(title: "Zoo", pageView: 5, sessionDuration: 2.0),
BlogPost(title: "Abena", pageView: 4, sessionDuration: 10.0),
BlogPost(title: "Alice", pageView: 1, sessionDuration: 3.0),
BlogPost(title: "Angero", pageView: 1, sessionDuration: 2.0),
BlogPost(title: "Peter", pageView: 1, sessionDuration: 2.0),
BlogPost(title: "Kofi", pageView: 1, sessionDuration: 1.0)]
let popularPosts = BlogPost.examples2.sorted { (lhs, rhs) in
if lhs.pageView == rhs.pageView {
if lhs.sessionDuration == rhs.sessionDuration {
if lhs.nextCriteria == rhs.nextCriteria {
if lhs.nextCriteria == rhs.nextCriteria {
....
}
...
}
...
}
return lhs.sessionDuration > rhs.sessionDuration
}
return lhs.pageView > rhs.pageView
}
let sortedObjects = objects.sorted { (lhs, rhs) in
for (lhsCriteria, rhsCriteria) in [(lhsCrtria1, rhsCriteria1), (lhsCrtria2, rhsCriteria2), (lhsCrtria3, rhsCriteria3), ... , (lhsCrtriaN, rhsCriteriaN)] {
if lhsCriteria == rhsCriteria {
continue
}
return lhsCriteria < rhsCriteria
}
}
extension BlogPost {
static var examples2: [BlogPost] = [
BlogPost(title: "Zoo", pageView: 5, sessionDuration: 2),
BlogPost(title: "Alice", pageView: 1, sessionDuration: 3),
BlogPost(title: "Peter", pageView: 1, sessionDuration: 2),
BlogPost(title: "Kofi", pageView: 1, sessionDuration: 1),
BlogPost(title: "Akosua", pageView: 5, sessionDuration: 2),
BlogPost(title: "Abena", pageView: 4, sessionDuration: 10),
BlogPost(title: "Angero", pageView: 1, sessionDuration: 2)
]
}
typealias AreInIncreasingOrder = (BlogPost, BlogPost) -> Bool // ①
let popularPosts = BlogPost.examples2.sorted { (lhs, rhs) in
let predicates: [AreInIncreasingOrder] = [ // ②
{ $0.pageView > $1.pageView },
{ $0.sessionDuration > $1.sessionDuration},
{ $0.title < $1.title }
]
for predicate in predicates { // ③
if !predicate(lhs, rhs) && !predicate(rhs, lhs) { // ④
continue // ⑤
}
return predicate(lhs, rhs) // ⑥
}
return false
}
[BlogPost(title: "Akosua", pageView: 5, sessionDuration: 2.0),
BlogPost(title: "Zoo", pageView: 5, sessionDuration: 2.0),
BlogPost(title: "Abena", pageView: 4, sessionDuration: 10.0),
BlogPost(title: "Alice", pageView: 1, sessionDuration: 3.0),
BlogPost(title: "Angero", pageView: 1, sessionDuration: 2.0),
BlogPost(title: "Peter", pageView: 1, sessionDuration: 2.0),
BlogPost(title: "Kofi", pageView: 1, sessionDuration: 1.0)]
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/Forever_wj/article/details/121950242
内容来源于网络,如有侵权,请联系作者删除!