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 should add a method called `points` to your base class:

```
abstract class MyAbstractClass { def points:Int }

```

The subclasses already implement this method, since they are case classes, so you don't need to change them.

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

```

Then you can sort the list using this method.

```
println(List(Child2('a',0),Child1(Child2('b',2),3)).sortBy(_.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
}

```