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?
compareBy(productSortedOrder::indexOf), and then you can map the keys to their names afterwards if necessary.