欢迎使用CSDN-markdown编辑器

来源:互联网 发布:手机处方软件 编辑:程序博客网 时间:2024/05/17 08:33

初学,自己留给自己看,学习for循环,了解,list的sort算法

这里面要做的是将一个employee的list根据parent区分,按照budget来排序
这是第一次写,经验不足,排版各种乱到死,我也是心都碎了,慢慢记载,见证自己的一步步成长
scala,等着我来吃掉你。。。。。

package mainimport scala.collection.JavaConverters._/** * Created by ray on 15-6-15. */case class employee(val name: String, val department: Int, val parent: Int, val budget: Int) {  def getParent(index: Int, list: List[employee]): List[employee] = {    val list1 = for (      i <- list      if i.department == index    ) yield (i)    list1  }}object Exam {  def main(args: Array[String]) {    val list = List(employee("xiaohong", 1, 2, 90), employee("xiaobi", 2, 0, 123), employee("ray", 2, 1, 345), employee("young", 0, -1, 80), employee("xiaohonghong", 1, 2, 95), employee("xiaobixiaobi", 1, 0, 81), employee("xiaohong1", 1, 2, 91))    val ray = employee("ray", 2, 0, 60)    val list1 = ray getParent(ray parent, list)    var list3 = for (      i <- list    ) yield (i.parent)    //distinct    //    list3 foreach println//    list3.distinct foreach (println)    list3 = list3.distinct    //    list1 foreach(println)    //sortBy  sortWith  i love you    //    list.sortBy((i:employee)=>{i.budget    //      i.parent})    //    list.sortBy((i:employee)=>{i.parent    //      i.budget    //      }) foreach(println)    //    list.sortWith((i:employee,j:employee)=>i.budget>j.budget) foreach println    //得到一个按照parent区分的list    var list5 = List[employee]()    for (i <- list3) {      for (j <- list)        if (j.parent == i)          list5 = list5.::(j)    }    //将list根据budget来从小到大来排序    list5.sortWith((i: employee, j: employee) => i.budget > j.budget && i.parent == j.parent) foreach (println)  }}
0 0