Sorting Information with Comparable -Swift中实例的排序问题
来源:互联网 发布:面向对象编程的优势 编辑:程序博客网 时间:2024/06/08 20:08
Comparable 有两个先决条件: 类型要满足Equatable protocol和使用<操作符。
struct Employee: Equatable,Comparable {
var firstname: String var lastname: String var title: String
//相同实例的先决条件 static func == (lhs: Employee, rhs: Employee) -> Bool { return lhs.firstname == rhs.firstname && lhs.lastname == rhs.lastname && lhs.title == rhs.title }//排序依据 static func < (lhs:Employee, rhs:Employee) -> Bool { return lhs.lastname < rhs.lastname }}let employee1 = Employee(firstname: "Si", lastname: "li", title: "PL")let employee2 = Employee(firstname: "Paul", lastname: "maivd", title: "TL")let employee3 = Employee(firstname: "Lulu", lastname: "ain", title: "Sales")let employeeall = [employee1,employee2,employee3]//此处使用闭包的函数 - sorted(by:)let employeesorted = employeeall.sorted(by: <)for employee in employeesorted { print(employee)}
Employee(firstname: "Lulu", lastname: "ain", title: "Sales")Employee(firstname: "Si", lastname: "li", title: "PL")Employee(firstname: "Paul", lastname: "maivd", title: "TL")
添加Int,可以自由排序(升序或者降序)
struct Employee: Equatable,Comparable { var firstname: String var lastname: String var title: String var age: Int static func == (lhs: Employee, rhs: Employee) -> Bool { return lhs.firstname == rhs.firstname && lhs.lastname == rhs.lastname && lhs.title == rhs.title } static func < (lhs:Employee, rhs:Employee) -> Bool { return lhs.age < rhs.age }}let employee1 = Employee(firstname: "Si", lastname: "li", title: "PL", age: 50)let employee2 = Employee(firstname: "Paul", lastname: "maivd", title: "TL", age: 40)let employee3 = Employee(firstname: "Lulu", lastname: "ain", title: "Sales", age: 60)let employeeall = [employee1,employee2,employee3]//此处使用的闭包的函数let employeesorted = employeeall.sorted(by: <)for employee in employeesorted { print(employee)}
输出结果:
Employee(firstname: "Paul", lastname: "maivd", title: "TL", age: 40)
Employee(firstname: "Si", lastname: "li", title: "PL", age: 50)
Employee(firstname: "Lulu", lastname: "ain", title: "Sales", age: 60)
如果将 sorted(by: <) 改为'>'
输出结果将随之发生变化:
Employee(firstname: "Lulu", lastname: "ain", title: "Sales", age: 60)
Employee(firstname: "Si", lastname: "li", title: "PL", age: 50)
Employee(firstname: "Paul", lastname: "maivd", title: "TL", age: 40)
阅读全文
1 0
- Sorting Information with Comparable -Swift中实例的排序问题
- Java Comparable 解决排序问题
- NSArray的sorting排序
- Java中comparable和comparator接口的排序原理
- Java中List的排序 Comparable和 Compartor
- [BOI2007]名次排序问题(sorting)
- java对象排序(Comparable)详细实例
- 数组排序的Java实现(Sorting of arrays implemented with Java)
- Java Sorting: Comparator vs Comparable
- Java Sorting: Comparator vs Comparable
- JAVA中关于对象数组是否可以实现Comparable接口和排序问题
- Comparable排序
- magento sorting varchar 字段排序问题
- 【排序】冒泡排序Bubble Sorting的特点
- 泛型的排序问题(Collections.sort及Comparable的应用)
- 排序(sorting)
- 排序 Sorting
- java Comparable 和 Comparator 的 Collections 排序
- 程序猿必须知道的一些有用的(外国)网站
- Codeforces758D---Ability To Convert
- 深度学习(一)深度学习学习资料
- 简洁Java之道
- Linux 学习-" "(双引号)与 ' '(单引号)的区别
- Sorting Information with Comparable -Swift中实例的排序问题
- Docker源码分析(四)之Docker Server
- Log4cpp 库安装及使用
- JS的面向对象-工厂方式-原型-继承
- 数据结构:链表队列
- Another a+b FZU
- 基于S3C2440的busybox的移植---解决FL2440的shell指令不能自动补全
- div模拟textarea高度随文本变化
- 一个手风琴效果的jquery动画