I'm having some problems with the following code:
object GestorContactos {
private var listaContantocs:List[Contacto] = Nil
def insertarOrd(c:Contacto):List[Contacto]=listaContantocs={
var aux:List[Contacto]=listaContantocs.sortBy(_.edad)
aux.filter(x=>x.edad<c.edad)::c::(aux.filter(x=>x.edad>c.edad))
}
aux.filter(x=>x.edad<c.edad)
returns List[Contacto] and the same does aux.filter(x=>x.edad>c.edad)
.
The problem is that when I try to add both results with a Contacto
Object (var c) on a single list the following error appears:
type mismatch;
found : List[Contacto]
required: Contacto
It seems that listaContantocs
, a inmutable List[Contacto], does not like the return type List[Object]. Is there any way I could cast List[Object] to List[Contacto]?
You are combing different element types List[Contacto]
and Contacto
with operator ::
.
The following is what you want
aux.filter(x=>x.edad<c.edad) ++ (c :: (aux.filter(x=>x.edad>c.edad)))