I have a Map and a List. The List should be sorted based on the Map key values. E.g:
Map = (<2,"Andy">,<4,"Karl">)
List = ("Kathy","Andy","Yiri","Jun","Karl")
I have to sort the List in such a way that at index : (2 : Andy) should be there
and at index (4 : Karl) should be there. For rest of elements order does not matter.
Rest of Conditions are :
- Entries in Map may or may not present in List ;(In this we can keep the order same)
- List could be empty
- Map could be empty
I am able to do this in 2 loops , I was curious to know if it is possible to achieve this in a single Loop.
You can use 1 loop to loop through all keys in the map, and search for the object in the List with
indexOf(Object), then usesettwice to swap the object to the correct position.Pseudocode (that looks like Java):
Well, complexity is not that good: O(nk) where n is the number of elements in the List and k is the number of elements in the Map.