Solved Hashmaps from highest to lowest

Discussion in 'Plugin Development' started by benzimmer123, Mar 1, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    benzimmer123

    I have tried a few things but doesn't work so any help would be much appreciated. This is my current code, config and what it prints in game...

    Code:
        HashMap<String, Integer> players = new HashMap<String, Integer>();
    
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, final String args[]) {
            Player p = (Player) sender;
            if (cmd.getName().equalsIgnoreCase("top")) {
    
                SortingScores();
               
                sortByValue(players);
    
                String firstname = (String) players.keySet().toArray()[0];
                int firstscore = (int) players.values().toArray()[0];
    
                String secondname = (String) players.keySet().toArray()[1];
                int secondscore = (int) players.values().toArray()[1];
    
                String thirdname = (String) players.keySet().toArray()[2];
                int thirdscore = (int) players.values().toArray()[2];
    
                p.sendMessage("Top Players: ");
                p.sendMessage(firstname + ": " + firstscore);
                p.sendMessage(secondname + ": " + secondscore);
                p.sendMessage(thirdname + ": " + thirdscore);
    
            }
            return false;
        }
    
        public void SortingScores() {
            for (String s : plugin.settings.getKills().getConfigurationSection("Kills").getKeys(false)) {
                players.put(s, plugin.settings.getKills().getInt("Kills." + s));
            }
        }
       
    
        public static Map<String, Integer> sortByValue(Map<String, Integer> map) {
            List<Map.Entry<String, Integer>> list = new LinkedList<Map.Entry<String, Integer>>(map.entrySet());
    
            Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
    
                public int compare(Map.Entry<String, Integer> m1, Map.Entry<String, Integer> m2) {
                    return (m2.getValue()).compareTo(m1.getValue());
                }
            });
    
            Map<String, Integer> result = new LinkedHashMap<String, Integer>();
            for (Map.Entry<String, Integer> entry : list) {
                result.put(entry.getKey(), entry.getValue());
            }
            return result;
        }
    The config file:

    Code:
    Kills:
      Test1: 5
      Test2: 2
      Test3: 3
      Test4: 9
    How it appears in game:
    Code:
    TopPlayers:
    Test1: 5
    Test4: 9
    Test3: 3
     
  2. Offline

    PreFiXAUT

    @benzimmer123 If you want the Keys sorted -> Map#getKeys(), save them into a List (use it as parameter in the constructor or use List#addAll), then Collections#sort(yourListHere) and you should be fine.

    If you want the Values sorted, use Map#getValues() and do the same (Can oviously only be used if you only need the Values). When the Collections#sort() Method doesn't do it's work like you need it, make a custom sort method :)
     
  3. Offline

    Konato_K

    @benzimmer123 You're not using the Map returned by sortByValue, you're calling the method and expecting the "players" Map changes, which will never work.

    Also, you are creating a new array from the value set for no reason.
     
  4. Offline

    benzimmer123

    @Konato_K @PreFiXAUT
    Ah, this seems to work fine now. Thanks very much! I just added this line

    Code:
    players = (HashMap<String, Integer>) sortByValue(players);
    How else would I get the top 3 scores by not adding them to an arraylist so I don't have to print every single score?
     
  5. Offline

    PreFiXAUT

    @benzimmer123 What do you mean? Why print every single Score?
     
  6. Offline

    benzimmer123

  7. Offline

    PreFiXAUT

    He ment this one I guess.
     
  8. Offline

    benzimmer123

    @PreFiXAUT

    Oh but don't I need to convert it from a map to a list?
     
  9. Offline

    PreFiXAUT

    @benzimmer123 Yea, but not with the entries. You use the Keys instead. Entries aren't getting sorted properly I think (I guess it uses the hashCode() Method to sort them, so that could case problems).
     
Thread Status:
Not open for further replies.

Share This Page