Sorting Hashmap by values?

Discussion in 'Plugin Development' started by AwesomeFishh, May 19, 2018.

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

    AwesomeFishh

    Hey, so I have to sort a hashmap by its values. I've tried googling but the few things I found were quite complex, so I was hoping someone here could give me a simpler explanation.

    What I want to do:

    I have a config file with player uuids and a corresponding long value.
    I want this to be sorted by the long value, from higher to lower, and I only need the top 20 values.
     
  2. Online

    timtower Administrator Administrator Moderator

    @AwesomeFishh Clone the map.
    Loop over it and find the highest value.
    Store the key in a new list.
    Remove the key from the cloned map.
    Repeat 19 more times.
    Print everything (or do other stuff with it)
     
  3. Offline

    AwesomeFishh

    @timtower Thanks for your reply, I implemend it in my code but doesnt work.

    This is what I have now. Problem: When testing this with my 2 accounts: AwesomeFishhy, and AFishhy, it displays only AFishhy as: 2. AFishhy: 0 days, 0 hours, 0 minutes, 0 seconds

    The plugin.getDurationBreakdown is not the problem, as I have that perfectly working with another command.
    Also, AwesomeFishhy doesnt show up as nr. 1.

    Do you know why this happens, I can't figure it out.

    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String s, String[] args) {
    
            if (cmd.getName().equalsIgnoreCase("zilanttop")) {
    
                // Get top20 long values (time played) and sort from high to low
                if (sender instanceof Player) {
                    Player player = (Player) sender;
                    plugin.updateTimePlayed();
                    if (args.length == 0) {
                        int counter = 0;
                        HashMap<Player, Long> players = new HashMap<Player, Long>();
                        for (String key : plugin.getConfig().getConfigurationSection("players").getKeys(false)) {
                            counter++;
                            UUID pUuid = UUID.fromString(key);
                            Player p = Bukkit.getPlayer(pUuid);
                            players.put(p, plugin.getConfig().getLong(key));
                        }
    
                        HashMap<Player, Long> clone = new HashMap<Player, Long>(players);
                        HashMap<Player, Long> sorted = new HashMap<Player, Long>();
                        Long highest = (long) 0;
                        String highestP = "";
                        if (counter > 20) {
                            counter = 20;
                        }
                        for (int i = 0; i < counter; i++) {
                            for (Player key1 : clone.keySet()) {
                                Long value = players.get(key1);
                                if (value > highest) {
                                    highest = value;
                                    highestP = key1.getName();
                                }
                            }
                            clone.remove(Bukkit.getPlayer(highestP));
                            sorted.put(Bukkit.getPlayer(highestP), highest);
                        }
                        int counter2 = 0;
                        for (Player key2 : sorted.keySet()) {
                            player.sendMessage(plugin.prefix + " " + counter + ". " + key2.getName() + ": "
                                    + plugin.getDurationBreakdown(sorted.get(key2)));
                        }
                    } else {
                        player.sendMessage(plugin.prefix + " Too many arguments! /zilanttop");
                    }
                } else {
                    sender.sendMessage(plugin.prefix + " Only players can use this command!");
                }
            }
    
            return false;
        }
     
  4. Online

    timtower Administrator Administrator Moderator

    @AwesomeFishh Ordered thing needs to be a list.
    A hashmap is always sorted on key.
     
Thread Status:
Not open for further replies.

Share This Page