List
has a method, sortWith
, which sorts your list, where you can provide the criteria for sorting the list. The criteria is a function, accepting two arguments (two profiles), and result is Boolean, which indicates which one of them is "greater".
So, you can do the following:
ll.sortWith((p1, p2) =>
getCount(p1) > getCount(p2)
)
where
def getCount(profil: Profil) = {
var count = profil.czasOgladania.get
if(profil.czyKupil.get) count = count *4 ;
if(profil.czyPrzeczytal.get) count = count *3 ;
if(profil.czyWKarcie.get) count = count *2 ;
count
}
Update
BTW, it seems that profil.czasOgladania
, profil.czyKupil
etc., are Option
s. In that case you should first check if they are defined, and than perform computations. You may define default values, e.g.
// if profil.czasOgladania is defined, get the value. Else, return 10.
val count = profil.czasOgladania.getOrElse(10)
or:
if(profil.czyWKarcie.getOrElse(false)) count = count *2