欢迎使用CSDN-markdown编辑器

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

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

package main

import 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)

  }


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值