当前位置:网站首页>Swift--多条件排序
Swift--多条件排序
2022-08-09 18:19:00 【锐意无限】
关于排序我们都知道swift有专门提供的函数sort
给我们使用,但是sort
这个函数只能对单条件进行排序,那如果我们遇到多条件和多属性的时候怎么进行排序呢?我们进行就研究下
首先我们创建一个Person
结构来进行测试验证
person
包含名、姓、和生日,我们下面会对这三个属性进行联合排序
struct Person {
let first: String // 姓
let last: String // 名
let yearOfBirth: Int // 生日
init(first: String, last: String, yearOfBirth: Int) {
self.first = first
self.last = last
self.yearOfBirth = yearOfBirth
// super.init() 在这里被隐式调用
}
}
我们先创建一个数组:
let people = [
Person(first: "Emily", last: "Young", yearOfBirth: 2002),
Person(first: "David", last: "Gray", yearOfBirth: 1991),
Person(first: "Robert", last: "Barnes", yearOfBirth: 1985),
Person(first: "Ava", last: "Barnes", yearOfBirth: 2000),
Person(first: "Joanne", last: "Miller", yearOfBirth: 1994),
Person(first: "Ava", last: "Barnes", yearOfBirth: 1998)
]
然后我们对这个数组进行排序,规则是先按照姓排序,再按照名排序,最后是出生年份
这样我们就是三个条件联合排序了
首先我们要理清这个排序的逻辑:
多条件排序的判断方式是:先比较第一个条件的排序,只有当第一个条件相等时候,才转到下一个条件去判断,直到找到一个不相等的条件
下面我们根据这个逻辑开始谢代码:
let newPeople = people.sorted {
lhs, rhs in
// 我们先判断第一个条件
if lhs.first == rhs.first {
// 当第一个添加相等的时候才去判断下一个条件,以此类推
if lhs.last == rhs.last {
return lhs.yearOfBirth < rhs.yearOfBirth
}
return lhs.last < rhs.last
}
// 不相等的时候判断排序
return lhs.first < rhs.first
}
print( newPeople)
这时候打印是:
[Algorithm.Person(first: "Ava", last: "Barnes", yearOfBirth: 1998),
Algorithm.Person(first: "Ava", last: "Barnes", yearOfBirth: 2000),
Algorithm.Person(first: "David", last: "Gray", yearOfBirth: 1991),
Algorithm.Person(first: "Emily", last: "Young", yearOfBirth: 2002),
Algorithm.Person(first: "Joanne", last: "Miller", yearOfBirth: 1994),
Algorithm.Person(first: "Robert", last: "Barnes", yearOfBirth: 1985)]
三个条件的排序我们知道了,但是如果是N个条件呢,这时候我们会在里面写很多if else
,这明显不符合我们的开发效率,所以我们可以改编下上面的代码,我们把N个条件进行数组话,放在一个数组里面,然后去遍历这个数组中的条件进行判断,看下行不:
// 首先我们定个闭包别名
typealias AreInIncreasingOrder = (Person,Person) -> Bool
let newPeople = people.sorted {
lhs, rhs in
// 条件联合
// 首要的条件放第一个,按规则的循序添加排序条件
let predicates:[AreInIncreasingOrder] = [
{
$0.first < $1.first },
{
$0.last < $1.last },
{
$0.yearOfBirth < $1.yearOfBirth }
]
for predicate in predicates {
if !predicate(lhs, rhs) && !predicate(rhs,lhs) {
continue
}
return predicate(lhs,rhs)
}
return false
}
print(newPeople)
打印结果:
[Algorithm.Person(first: "Ava", last: "Barnes", yearOfBirth: 1998),
Algorithm.Person(first: "Ava", last: "Barnes", yearOfBirth: 2000),
Algorithm.Person(first: "David", last: "Gray", yearOfBirth: 1991),
Algorithm.Person(first: "Emily", last: "Young", yearOfBirth: 2002),
Algorithm.Person(first: "Joanne", last: "Miller", yearOfBirth: 1994),
Algorithm.Person(first: "Robert", last: "Barnes", yearOfBirth: 1985)]
结果跟上面的方法打印的结果一样,完美!
边栏推荐
猜你喜欢
工大科雅深交所上市:市值45亿 齐承英家族是大股东
数学建模——模拟退火
uniapp离线推送华为厂商申请流程
什么是藏宝计划(TPC),2022的一匹插着翅膀的黑马!
Samsung's flagship discount is 1,800, Apple's discount is over 1,000, and the domestic flagship is only reduced by 500 to send beggars
Bi Sheng Compiler Optimization: Lazy Code Motion
全自动化机器学习建模!效果吊打初级炼丹师!
太厉害了!华为大牛终于把 MySQL 讲的明明白白(基础 + 优化 + 架构)
鹅厂机器狗花式穿越10m梅花桩:前空翻、单桩跳、起身作揖...全程不打一个趔趄...
leetcode 503.下一个更大元素II 单调栈
随机推荐
Start cleaning up the long-term divers in the electronic chart development group again
国内市场上的 BI 软件到底有啥区别?
毕昇编译器优化:Lazy Code Motion
[免费专栏] Android安全之ZIP文件目录遍历漏洞
与同步传递相关的获取-释放序列
2022深圳(软考中级)系统集成项目管理工程师报名
数学建模——模拟退火
loadrunner script -- parameterization
From functional testing to automated testing, do you know their shortcomings?
5.4 总结
使用.NET简单实现一个Redis的高性能克隆版(四、五)
21天学习挑战赛--第四天打卡(横竖屏切换)
2021 RoboCom 世界机器人开发者大赛-本科组(决赛)
韩国严厉监管元宇宙相关企业
shell脚本基础语句使用(一)
程序健壮性
PHP基础笔记-NO.4
哈希表
LeetCode笔记:Weekly Contest 305
Fully automated machine learning modeling!The effect hangs the primary alchemist!