Sorting strings with assigned Integers

Discussion in 'Plugin Development' started by Zobru, Jan 18, 2021.

  1. Offline

    Zobru

    Hi everybody.

    I'm currently trying to solve a problem and have been searching around the web for hours without finding a solution.

    I'm trying to get all the players every joined on my server, convert them/their uuid to a string and have my plugin search a config. In this config, there are informations saved in this scheme:

    Code:
    UUID1HERE:
      name: NAME1
      count: 1
    UUID2HERE:
      name: NAME2
      count: 2
    This is my code right now, it works, but without sorting or saving the outcome.
    Code:
    FileConfiguration config_toplist = YamlConfiguration.loadConfiguration(toplist);
                        for (OfflinePlayer op : Bukkit.getOfflinePlayers()) {
                            String op_uuid_string = op.getUniqueId().toString();
                            if (config_toplist.isConfigurationSection(op.getUniqueId().toString())) {
                                ConfigurationSection section = config_toplist.getConfigurationSection(op.getUniqueId().toString());
                                String op_name = section.get("name").toString();
                                int op_count = section.getInt("count");
                               
                               
                            }
                           
                           
                        }
    Now I want to say - if my plugin does find the uuid in this config, than it should save name and count, assigned together in a way that I can sort this list in the end to get a toplist.

    I want to find out, which name does have the highest count.
    Help please..
     
  2. Offline

    CraftCreeper6

    @Zobru
    config#set("path.to.value", value)

    Make sure to
    config#save(file) afterwards.
     
  3. Offline

    Zobru

    That doesn't solve my problem at all.
    I am trying to sort the outcomes of said code..

    I now put them in a LinkedHashMap and am trying to sort the outcome and limit the output to 10 entrys..
     
  4. Offline

    CraftCreeper6

    @Zobru
    Apologies, didn't quite understand your question when I first read it.

    Create a value that stores the 'highest count', set it to 0 initially, loop through each player online and check the config for their value, if the value is greater than the current 'highest count', set the 'highest count' to be equal to the value that the player has. Make sure to store the UUID too though else all you'll have is a number.
     
  5. Offline

    ForbiddenSoul

    If you just want the one top person, you can simply loop through the keys and values in your code with something like
    SUDO CODE:
    Code:JAVA
    1. String topPlayer;
    2. int topValue = -1;
    3. for (String value : configKeys) {
    4. if(get config section for value. get config section for count as int > topValue) {
    5. topValue = get config section for value. get config section for count as int;
    6. topPlayer = get config section for value. get config section for name as string;
    7. }
    8. }

    Then you know the top player.
    If you need all of the top people sorted by what their count is use a SortedMap.
     
  6. Offline

    Strahan

    OP, as you are working with config data that already has the peoples' UUIDs/names/counts I don't see why you care to iterate the offline players. It's irrelevant as you already have the data. I'd read the config data into a Map<String, Integer> then sort it by the values to get what you want if you want more than just the single top player. There are a plethora of examples online for making the function to sort the map, it's not difficult.

    One caveat to your pseudocode; you really shouldn't promote hanging methods off of other methods that may return null. I code from the assumption that the admin is an idiot and messed up the config and make my plugins able to gracefully handle such things. If you don't null check the method, if the section is missing it will puke.

    Also the SortedMap works on keys, so that wouldn't be terribly useful here. I ran into this in the past, and from what I read there isn't really a straight forward way to sort by values, you apparently have to make a function to do it.
     

Share This Page