I have a simple map and need to create a list that is sorted based on the number in ascending order from a given list:
Map auto = new HashMap();
auto.put("Merc", 3);
auto.put("Citroen", 5);
auto.put("Opel", 10);
auto.put("BMW", 20);
List<String> given = new ArrayList<>();
given.add("Opel");
given.add("BMW");
given.add("Citroen");
So the given list needs to be sorted so that it will be in this order: Citroen, Opel, BMW. Was thinking of:
- create another map then iterate through list
- get number from first map
- put the number as the key and the name as value in the new map
- sort the map by key
- iterate threw new map then add values to the list
This seems terrible :/, any suggestions and perhaps better data structures to use?