I have a list of maps as shown below. I want to sort maps inside a list in custom order in Java 8.
For example, Below is a list of maps.
[{model=Ferrari},
{model=Tesla},
{model=Benz},
{model=Honda}]
If I sort the list of cards above using the code below, it is sorted in alphabetical order.
l.sort(Comparator.comparing((Map<String,String> mp) -> mp.get("model")));
gives the result below.
[{model = Benz}, {model = Ferrari}, {model = Honda}, {model = Tesla}]
But I need to sort this card based on the order (not in alphabetical order): Honda, Tesla, Benz, Ferrari
Is there a better approach to achieving custom sorting in Java 8?
source
share