代码改变世界

scala 排序

2018-08-11 17:12  xplorerthik  阅读(119)  评论(0编辑  收藏  举报

sortBy()

定义:

def sortBy[B](fun: (A) =>B)

栗子1:

val words = "the quick brown fox jumped over the lazy dog"

val words_split = words.split(" ")

words_split.sortBy(x =>(x.length, x.head) )  // 按照右边的(x.length, x.head)进行排序

output:  The, dog, fox, the, lazy, over, brown, quick, jumped

栗子2:

val myseq1 = Array((3,1,2,5),(2,4,5,1),(8, 2, 4,9)

myseq1.sortBy(_._1).foreach(println)