sortBy: sortBy[B](f: (A) ⇒ B)(implicit ord: math.Ordering[B]): List[A] 按照应用函数f之后产生的元素进行排序

sorted: sorted[B >: A](implicit ord: math.Ordering[B]): List[A] 按照元素自身进行排序

sortWith: sortWith(lt: (A, A) ⇒ Boolean): List[A] 使用自定义的比较函数进行排序,比较函数boolean

用法

<code class="hljs coffeescript has-numbering">val nums = List(<span class="hljs-number">1</span>,<span class="hljs-number">3</span>,<span class="hljs-number">2</span>,<span class="hljs-number">4</span>)
val sorted = nums.sorted  <span class="hljs-regexp">//</span>List(<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>)val users = List((<span class="hljs-string">"HomeWay"</span>,<span class="hljs-number">25</span>),(<span class="hljs-string">"XSDYM"</span>,<span class="hljs-number">23</span>))
val sortedByAge = users.sortBy{<span class="hljs-reserved">case</span><span class="hljs-function"><span class="hljs-params">(user,age)</span> =></span> age}  <span class="hljs-regexp">//</span>List((<span class="hljs-string">"XSDYM"</span>,<span class="hljs-number">23</span>),(<span class="hljs-string">"HomeWay"</span>,<span class="hljs-number">25</span>))
val sortedWith = users.sortWith{<span class="hljs-reserved">case</span><span class="hljs-function"><span class="hljs-params">(user1,user2)</span> =></span> user1._2 < user2._2} <span class="hljs-regexp">//</span>List((<span class="hljs-string">"XSDYM"</span>,<span class="hljs-number">23</span>),(<span class="hljs-string">"HomeWay"</span>,<span class="hljs-number">25</span>))</code>

How to sort a Scala Map by key or value (sortBy, sortWith)

By Alvin Alexander. Last updated: Jun 6, 2015

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 11.23, “How to Sort an Existing Map by Key or Value”

Problem

You have an unsorted map and want to sort the elements in the map by the key or value.

Solution

Given a basic, immutable Map:

scala> val grades = Map("Kim" -> 90,|     "Al" -> 85,|     "Melissa" -> 95,|     "Emily" -> 91,|     "Hannah" -> 92| )
grades: scala.collection.immutable.Map[String,Int] = Map(Hannah -> 92, Melissa -> 95, Kim -> 90, Emily -> 91, Al -> 85)

You can sort the map by key, from low to high, using sortBy:

scala> import scala.collection.immutable.ListMap
import scala.collection.immutable.ListMapscala> ListMap(grades.toSeq.sortBy(_._1):_*)
res0: scala.collection.immutable.ListMap[String,Int] = Map(Al -> 85, Emily -> 91, Hannah -> 92, Kim -> 90, Melissa -> 95)

You can also sort the keys in ascending or descending order using sortWith:

// low to high
scala> ListMap(grades.toSeq.sortWith(_._1 < _._1):_*)
res0: scala.collection.immutable.ListMap[String,Int] = Map(Al -> 85, Emily -> 91, Hannah -> 92, Kim -> 90, Melissa -> 95)// high to low
scala> ListMap(grades.toSeq.sortWith(_._1 > _._1):_*)
res1: scala.collection.immutable.ListMap[String,Int] = Map(Melissa -> 95, Kim -> 90, Hannah -> 92, Emily -> 91, Al -> 85)

You can sort the map by value using sortBy:

scala> ListMap(grades.toSeq.sortBy(_._2):_*)
res0: scala.collection.immutable.ListMap[String,Int] = Map(Al -> 85, Kim -> 90, Emily -> 91, Hannah -> 92, Melissa -> 95)

You can also sort by value in ascending or descending order using sortWith:

// low to high
scala> ListMap(grades.toSeq.sortWith(_._2 < _._2):_*)
res0: scala.collection.immutable.ListMap[String,Int] = Map(Al -> 85, Kim -> 90, Emily -> 91, Hannah -> 92, Melissa -> 95)// high to low
scala> ListMap(grades.toSeq.sortWith(_._2 > _._2):_*)
res1: scala.collection.immutable.ListMap[String,Int] = Map(Melissa -> 95, Hannah -> 92, Emily -> 91, Kim -> 90, Al -> 85)

In all of these examples, you’re not sorting the existing map; the sort methods result in a new sorted map, so the output of the result needs to be assigned to a new variable.

Also, you can use either a ListMap or a LinkedHashMap in these recipes. This example shows how to use a LinkedHashMap and assign the result to a new variable:

scala> val x = collection.mutable.LinkedHashMap(grades.toSeq.sortBy(_._1):_*)
x: scala.collection.mutable.LinkedHashMap[String,Int] =Map(Al -> 85, Emily -> 91, Hannah -> 92, Kim -> 90, Melissa -> 95)scala> x.foreach(println)
(Al,85)
(Emily,91)
(Hannah,92)
(Kim,90)
(Melissa,95)

scala sortBy and sortWith相关推荐

  1. 【大数据开发】scala——tuple、list(含高阶方法)、wordcount案例、set、并行处理数据和sorted、sortBy、sortWith的区别

    文章目录 一.元组 1.1元组的定义.遍历 1.2元组的拉链.解链 二.list 2.1空list.list初始化.list遍历3种方式 2.2list的追加.拼接操作 2.3list的基本操作 2. ...

  2. scala中 sorted,sortBy,sortWith语法使用

    排序 在scala集合中,可以使用以下几种方式来进行排序 sorted 默认排序 sortBy 指定字段排序 sortWith 自定义排序 默认排序 sorted //示例:对列表进行升序排序 sca ...

  3. scala中sortBy与sortWith区别

    (1)Scala中sortBy是以方法的形式存在的,并且是作用在Array或List集合排序上,并且这个sortBy默认只能升序,除非实现隐式转换或调用reverse方法才能实现降序. (2)sort ...

  4. scala中sorted,sortby,sortwith的用法(转)

    scala中sorted,sortWith,sortBy用法详解 2017年07月23日 23:07:51 bitcarmanlee 阅读数:9249 版权声明:本文为博主原创文章,未经博主允许不得转 ...

  5. Scala 排序函数 sorted sortBy sortWith

    排序方法在实际的应用场景中非常常见,Scala里面有三种排序方法,分别是: sorted,sortBy ,sortWith 分别介绍下他们的功能: (1)sorted 对一个集合进行自然排序,通过传递 ...

  6. 2021年大数据常用语言Scala(二十):函数式编程 介绍

    目录 函数式编程 介绍 函数式编程的意义在哪? 函数式编程 介绍 我们将来使用Spark/Flink的大量业务代码都会使用到函数式编程.下面的这些操作是学习的重点. 现在我们将会逐渐接触函数式编程的方 ...

  7. Scala比较器:Ordered与Ordering

    在项目中,我们常常会遇到排序(或比较)需求,比如:对一个Person类 case class Person(name: String, age: Int) {override def toString ...

  8. scala里集合排序函数的使用

    2019独角兽企业重金招聘Python工程师标准>>> 排序方法在实际的应用场景中非常常见,Scala里面有三种排序方法,分别是: sorted,sortBy ,sortWith 分 ...

  9. Scala学习笔记(黑马视频)

    目录 2.Scala第二章节 2.1 输出语句和分号 2.1.1 输出语句 2.1.2 分号 2.2 Scala中的常量 2.2.1 概述 2.2.2 分类 2.2.3 代码演示 2.3. Scala ...

最新文章

  1. 老大,你为什么在代码中要求我们使用LocalDateTime而不是Date?
  2. java html字符串,java字符串方法
  3. 基于vue-cli配置手淘的lib-flexible + rem,实现移动端自适应
  4. iOS 钥匙串的基本使用
  5. 论文浅尝 - ACL2020 | 用于回答知识库中的多跳复杂问题的查询图生成方法
  6. Java Package getPackage()方法与示例
  7. js数组去重的四种方式
  8. Git Stash解释:如何在Git中临时存储本地更改
  9. 机器人砖机视频_全自动透水砖机生产线需严格遵守的标准工艺流程
  10. 金蝶云星空使用WebAPI来新增单据
  11. requests爬取免费代理2
  12. 技术研究院006---B站自用的微服务框架——Kratos
  13. php 读csv跳过标题,请问怎么使用Python编辑csv文件时跳过标题
  14. 单本振与双本振台标文件的区别在哪里?
  15. 车牌识别、证件识别、汽车VIN识别在汽车服务门店中的实际应用
  16. 阿里云mysql空间不足_阿里云数据库MySQL系统文件导致实例空间满的解决办法
  17. vscode+markdown+pandoc写论文
  18. 本土实力派陈旭东出任IBM大中华区总经理,意外还是惊喜?
  19. ps基础知识学习总结
  20. photon 服务器操作系统,[专栏作家] Photon Server之Photon Control服务器控制界面

热门文章

  1. 在线学习算法FTRL基本原理
  2. rtsp 报文转发_stp技术回顾和rstp经典笔记
  3. 当代著名国际摄影师相关网站大集合
  4. 指定locale为en US
  5. zzulioj1008: 美元和人民币
  6. 微软这个系统,90% 的人都没用过!
  7. 图像去噪——Neighbor2Neighbor: Self-Supervised Denoising from Single Noisy Images
  8. 2006设计师必备网址全集
  9. git pull设置用户名密码
  10. CSS基础(复合选择器-三大特性)