Skip to Content
Kotlin 编程实践
book

Kotlin 编程实践

by Ken Kousen
July 2020
Intermediate to advanced
235 pages
3h 37m
Chinese
China Machine Press
Content preview from Kotlin 编程实践
集合
103
类(非数据类),也可以手动定义任何所需要的 component 方法。
解构是从对象中提取多个元素的便捷方法。目前,List 类为前5 个元素定义了
component 函数。但这也许会在之后的 Kotlin 版本中发生改变。
5.9
将多个属性排序
问题
如何按第一个属性对一个类的对象进行排序,然后对第一个属性相同的对象按第二个属
性进行排序,以此类推。
解决方案
使用 sortedWith 函数与 compareBy 函数。
讨论
假设我们有一个名为 Golfer(高尔夫球手)的简单数据类,示例 5-24 中展示了一个简
单的集合。
示例 5-24:一个数据类以及一些简单的数据
data class Golfer(val score: Int, val first: String, val last: String)
val golfers = listOf(
Golfer(70, "Jack", "Nicklaus"),
Golfer(68, "Tom", "Watson"),
Golfer(68, "Bubba", "Watson"),
Golfer(70, "Tiger", "Woods"),
Golfer(68, "Ty", "Webb")
)
如果你想将这些高尔夫球手依据得分(score)来进行排序,然后使用姓氏(last name
对得分相同的高尔夫球手进行排序,最后使用名字(first name)对得分与姓氏均相同的
高尔夫球手进行排序,可以使用示例 5-25 中的代码。
示例 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

GraphQL 学习指南

GraphQL 学习指南

Eve Porcello, Alex Banks
Java 技术手册(原书第7版)

Java 技术手册(原书第7版)

Benjamin J. Evans, David Flanagan
Python机器学习基础教程

Python机器学习基础教程

Andreas C. Müller, Sarah Guido

Publisher Resources

ISBN: 9787111659624