博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
scala sortBy and sortWith
阅读量:6908 次
发布时间:2019-06-27

本文共 3330 字,大约阅读时间需要 11 分钟。

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

用法

val nums = List(1,3,2,4)val sorted = nums.sorted  //List(1,2,3,4)val users = List(("HomeWay",25),("XSDYM",23))val sortedByAge = users.sortBy{case(user,age) => age}  //List(("XSDYM",23),("HomeWay",25))val sortedWith = users.sortWith{case(user1,user2) => user1._2 < user2._2} //List(("XSDYM",23),("HomeWay",25))

 

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

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.ListMapimport 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 highscala> 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 lowscala> 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 highscala> 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 lowscala> 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) 转:https://blog.csdn.net/mengxing87/article/details/51636080 有关 sortWith 底层,请看这里:
 
你可能感兴趣的文章
android左侧滑效果
查看>>
Check_mk 主机状态为 down 但是主机下其他服务有数据且正常 解决方法
查看>>
解决重装 Windows 后进入不了 Linux
查看>>
获取系统串口号
查看>>
redis 并发处理,多线程以及synchronized锁的应用
查看>>
Ubuntu安装OpenSSL
查看>>
检查HP服务器内存状态脚本
查看>>
string.punctuation
查看>>
Linux运维 第二阶段 (十二) 进程管理
查看>>
在MWC2018上看工业互联网走向
查看>>
互联网产品常用英语单词
查看>>
实验一 分治与递归—用分治法实现元素选择 java算法
查看>>
Linux常用命令大全(归类)
查看>>
HMGET key field [field ...]
查看>>
shell习题-判断函数
查看>>
Redis Modules: an introduction to the API
查看>>
centos ip使用
查看>>
influxdb查询写入操作
查看>>
linux命令——mv
查看>>
Keepalived+LVS的简单应用
查看>>