I want to cast my list to the class type that I receive from the method argument.
basically I want to order my list based on a field that exists in the ClassA. how to achieve this?
sealed class ParentClass
data class ClassA(fieldInClassType: Int) : ParentClass()
data class ClassB(fieldInClassType: Int) : ParentClass()
I have a list of ClassA and ClassB and I want to create a generic method to sort them. in my old code, I used when() but it looks ugly.
so I am trying to refactor the code in a generic method. This is what I have now
private fun <T: ParentClass>foo(data: List<T>, classType: Class<T>){
val list = data as classType
list.sortedBy{it.fieldInClassType}
}