Question: I have the following hierarchy in Scala

```
abstract class MyAbstractClass
case class Child1(alpha: MyAbstractClass, points: Int) extends MyAbstractClass
case class Child2(beta: Char, points: Int) extends MyAbstractClass

```

Now I have a list
List(MyAbstractClass type objects ... either Chil1 or Child2 )

I want to sort the above list based on points. How to write this in Scala?

Answer: 
You can use structural typing helps you here

```
list.sortBy{
  case x: {def points: Int} => x.points
}

```

sortBy takes a function that when applied to each element will return the value to sort by. The case statement says "if x is a type that has a "point" method, then return x.points
Used a slightly different approach 
1) Without adding points var to parent class
2) Without changing the hierarchy of classes 

list containing objects of type MyAbstractClass

```
list.sortWith((x,y) => point(x) < point(y))

def point(mylist: MyAbstractClass): Int = mylist match {
case Child1(a, p) => p
case Child2(b, p) => p
}

```