scala sortBy and sortWith
2016-06-11 12:42
330 查看
sortBy: sortBy(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
用法
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”
You can sort the map by key, from low to high, using
You can also sort the keys in ascending or descending order using sortWith:
You can sort the map by value using
You can also sort by value in ascending or descending order using
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
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, 2015This 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, immutableMap:
scala> [b]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.ListMap scala> 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
ListMapor a
LinkedHashMapin these recipes. This example shows how to use a
LinkedHashMapand 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)
相关文章推荐
- volley文件上传
- JavaScript地理位置信息API
- eclipse+maven+spring开发环境搭建
- leetcode 21. Merge Two Sorted Lists
- 团队二阶段冲刺个人工作总结9
- duplicate symbols for architecture arm64 (Xcode error)
- 回归——同步更新github.io
- mysql 表锁——读锁和写锁
- 飞机机翼的构造及其原理
- 软件开发之路是充满荆棘与挑战之路
- 【GOF23设计模式】_外观模式_公司注册流程_迪米特法则JAVA241
- Android属性之build.prop生成过程分析
- POJ2486 apple tree(树形DP)
- 艺术摄影--对焦与景深(2学时)--SDUST
- vs2013编译osg缺少mfc120d.lib
- 分页标签笔记
- linux下的C语言开发
- kylin cube测试时,报错:org.apache.hadoop.security.AccessControlException: Permission denied: user=root, access=WRITE, inode="/user":hdfs:supergroup:drwxr-xr-x
- 求1+2+3+...+n
- 八大排序算法