'How to sort listOfItem based on condition in Kotlin

This is my given code

var type1 listOf<Type> = 0 size;
var type2 listOf<Type> = {"Ait 45","Ait 46"}
var type3 listOf<Type> = {"Ait 47"}
var type4 listOf<Type> = 0
var type5 listOf<Type> = {"Ait 58","Ait 59"}

Using below code i am sorting it

val data = ArrayList<Product>()
        data.add(Product(it=1,type1))
        data.add(Product(it=2,type2))
        data.add(Product(it=3,type3))
        data.add(Product(it=4,type3))
        data.add(Product(it=5,type4))

data.sortedBy { it.type.get(0).value}

i am trying to sort based on type value but i am getting ArrayIndexoutBound exception because type1 and type4 having empty or null so i want to sort if such type of item should come at last based on inserted order please help me how to apply condition to check if type is empty or null i dont want sort those item simply i can keep at end of list .



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source