package tddmicroexercises.leaderboard;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

public class Leaderboard
{

    private final List<Race> races;

    public Leaderboard(Race... races)
    {
        this.races = Arrays.asList(races);
    }

    public Map<String, Integer> driverResults()
    {
        Map<String, Integer> results = new HashMap<>();
        for (Race race : this.races)
        {
            for (Driver driver : race.getResults())
            {
                String driverName = race.getDriverName(driver);
                int points = race.getPoints(driver);
                if (results.containsKey(driverName))
                {
                    results.put(driverName, results.get(driverName) + points);
                } 
                else
                {
                    results.put(driverName, race.getPoints(driver));
                }
            }
        }
        return results;
    }

    public List<String> driverRankings()
    {
        Map<String, Integer> results = driverResults();
        Map<Integer, String> sortedResults = new TreeMap<>();
        for (String driverName : results.keySet())
        {
            sortedResults.put(results.get(driverName), driverName);
        }
        List<String> resultsList = new ArrayList<>(sortedResults.values());
        Collections.reverse(resultsList);
        return resultsList;
    }
}

@@ -116,16 +116,45 @@
 ctions;%0A
+import java.util.Comparator;%0A
 import j
@@ -219,34 +219,8 @@
 Map;
-%0Aimport java.util.TreeMap;
 %0A%0Apu
@@ -884,33 +884,16 @@
       %7D 
-%0A                
 else%0A   
@@ -951,38 +951,26 @@
 erName, 
-race.getPoints(driver)
+0 + points
 );%0A     
@@ -1152,29 +1152,21 @@
     
-Map%3CInteger, 
+List%3C
 String%3E 
 sort
@@ -1165,91 +1165,144 @@
 ng%3E 
-sortedR
+r
 esults
+List
  = new 
-TreeMap%3C%3E();%0A        for (String driverName : results.keySet(
+ArrayList%3C%3E(results.keySet());%0A        Collections.sort(resultsList, new DriverByPointsDescendingComparator(results
 ))
+;
 %0A   
@@ -1310,87 +1310,136 @@
     
-%7B%0A   
+return resultsList;%0A
     
+%7D%0A%0A
     
- sortedResults.put(results.get(driverName), driverName);%0A    
+private static final class DriverByPointsDescendingComparator implements Comparator%3CString%3E%0A
     
-%7D
+%7B
 %0A   
@@ -1447,118 +1447,290 @@
     
-List%3CString%3E resultsList = new ArrayList%3C%3E(sortedResults.values());%0A        Collections.reverse(resultsList);%0A
+private final Map%3CString, Integer%3E results;%0A%0A        private DriverByPointsDescendingComparator(Map%3CString, Integer%3E results)%0A        %7B%0A            this.results = results;%0A        %7D%0A%0A        @Override%0A        public int compare(String driverName1, String driverName2)%0A        %7B%0A    
     
@@ -1740,29 +1740,90 @@
  return 
+-
 results
-List;%0A    %7D
+.get(driverName1).compareTo(results.get(driverName2));%0A        %7D%0A    %7D%0A
 %0A%7D%0A

fix Bug in Java, do not drop drivers with same points