Kotlin Custom sorting on Map

Viewed 428

I have map of this object Map<String, List>. It is coming from backend and I wanted to sort it on Key.

Key will be any of this: CHECKING,SAVINGS, CREDIT, LOAN.

I want to sort the map in this order.

Here is my code which is not working as expected.

 private val productType: Comparator<in String> = compareBy { name ->
        productSortedOrder.indexOf(AccountProductEnum.safeValueOf(name))
    }

 private val productSortedOrder = arrayOf(AccountProductEnum.CHECKING,
        AccountProductEnum.SAVINGS,
        AccountProductEnum.CREDIT,
        AccountProductEnum.LOAN)

Here is how I am sorting the map:

accountList.groupBy { account -> account.product.name }
                        .toSortedMap(productType)

With this logic, it is printing in this order CREDIT, CHECKING, LOAN, SAVINGS I want in this sequence CHECKING, SAVINGS, CREDIT, LOAN.

How should I achieve this?

0 Answers
Related