title: Not able to create nested Object in Scala
body: <p>I am having the List Of Objects with parent child reference </p>
  
  <pre><code>case class EmployeeLevel(id: Long, name: String, parentId: Long,
                           item: List[EmployeeLevel])
  
  val employeeList = List(
    EmployeeLevel(1L, "Manoj", 0L, Nil),
    EmployeeLevel(2L, "Sv", 1L, Nil),
    EmployeeLevel(3L, "Cp", 2L, Nil),
    EmployeeLevel(4L, "Manju", 3L, Nil))
  </code></pre>
  
  <p>How to create Tree Structure or  arrange them in a hierarchical Order Like</p>
  
  <pre><code>val employeeList = List(
    EmployeeLevel(1L, "Manoj", 0L, List(
      EmployeeLevel(2L, "Sv", 1L, List(
        EmployeeLevel(3L, "Cp", 2L, List(
          EmployeeLevel(4L, "Manju", 3L, Nil))))))))
  </code></pre>
  
  <p>or This is the final json that i should get </p>
  
  <pre><code>[
      {
          "id": 1,
          "name": "Manoj",
          "parentId": 0,
          "item": [
              {
                  "id": 2,
                  "name": "Sv",
                  "parentId": 1,
                  "item": [
                      {
                          "id": 3,
                          "name": "Cp",
                          "parentId": 2,
                          "item": [
                              {
                                  "id": 4,
                                  "name": "Manju",
                                  "parentId": 3,
                                  "item": [  ]                         
  
                              }
                          ]
                      }
                  ]
              }
          ]
      }
  ]
  </code></pre>
  
  <p>I tried with this:</p>
  
  <pre><code> def arrange(employee1:List[EmployeeLevel],accumulator:List[EmployeeLevel]): List[EmployeeLevel] ={
      @tailrec   def arrange1(employeeLevel:List[EmployeeLevel],accu:List[EmployeeLevel]):List[EmployeeLevel] ={
        if(employeeLevel.nonEmpty){
          val headd=employeeLevel.head
          val taill=employeeLevel.tail
          val tt=taill.filter(_.parentId==headd.id)
          val qq= List(headd.copy(item = tt))
          arrange1(taill,accu++qq)
        }else{
          accu
        }
  
      }
      arrange1(employee1,Nil)
    }
  </code></pre>
  
  <p>But its working for only one level</p>