scala 排序

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)

扫描二维码关注公众号,回复: 2698649 查看本文章

猜你喜欢

转载自www.cnblogs.com/xinping-study/p/9460352.html