Sort tree map values from biggest to smallest ?

Discussion in 'Plugin Development' started by ThunderWaffeMC, Jan 14, 2014.

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

    ThunderWaffeMC

    Hi. Whats the easiest way to sort key values in a TreeMap from biggest to smallest and loop through them to send to a player? Example:

    TreeMap contains 3 keys like so:
    - String1 | 2
    - String2 | 1
    - String3 | 3
    Player types command
    It sorts the values from biggest to smallest like so:
    - String3 | 3
    - String1 | 2
    - String2 | 1
    It loops through the keys and sends a message to the player like:

    Code:java
    1.  
    2. // for loop
    3. player.sendMessage(TreeMap);
    4.  


    and it should return to the player like so:

    "3"
    "2"
    "1"

    Thanks in advance!
     
  2. Offline

    werter318

    Code:java
    1.  
    2. TreeMap<String, Integer> map = new TreeMap<String, Integer>();
    3. map.put("werter318", 3);
    4. map.put("Runelink2", 1);
    5. map.put("baron951", 2);
    6. NavigableMap<String, Integer> descMap = map.descendingMap();
    7. Set<String> keySet = descMap.keySet();
    8. for(String key : keySet){
    9. int value = descMap .get(key);
    10. System.out.println(key + " - " + value);
    11. }


    There you go :)
     
    ThunderWaffeMC likes this.
  3. Offline

    ThunderWaffeMC

    werter318
    Thanks very much it's doing well but it's putting the key into alphabetical order instead of the value in descending number order. How can I change this?
     
  4. Offline

    werter318

    ThunderWaffeMC The code I posted works fine for me :-/... puts the value in descending order.
     
  5. Offline

    ThunderWaffeMC

    Are you sure? Does that mean it's printing this for you: ?

    3
    2
    1

    It may be a problem with my values.
     
  6. ThunderWaffeMC
    Yep, working fine for me too, just tested it. Prints out:
    Code:
    werter318 - 3
    baron951 - 2
    Runelink2 - 1
    If this doesn't work for you, then may I see how you use it?
     
  7. Did you make the map <Integer, String> ?
     
  8. Offline

    ThunderWaffeMC

    werter318
    Well I think it's because the values in my config file are set like so:

    Player1: '1'
    Player2: '3'
    Player3: '2'

    If I keep it like that, it responds with the value being 0. When I remove the apostrophes, the order stays the same. Any idea on what I can do?

    My TreeMap is string, integer: TreeMap<String, Integer> members = new TreeMap<String, Integer>();
    And so is my nav map: NavigableMap<String, Integer> descMap = members.descendingMap();

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
  9. you can even use Arrays.Sort
     
  10. Offline

    ThunderWaffeMC

    werter318 Assist Louis1234567890987654321 I found that it is actually putting the key in alphabetical order and not the value in number order. My test:

    Code:java
    1.  
    2. if(args[0].equalsIgnoreCase("test")) {
    3. if(args.length == 1) {
    4. plugin.getCustomConfig().set("Town.Funds.Creeper_Fever.Overall", 2);
    5. plugin.getCustomConfig().set("Town.Funds.Creeper_Fever.Current", 2);
    6. plugin.getCustomConfig().set("Town.Funds.Thunder_Waffe.Overall", 1);
    7. plugin.getCustomConfig().set("Town.Funds.Thunder_Waffe.Current", 1);
    8. plugin.getCustomConfig().set("Town.Funds.DiamondLeggings.Overall", 3);
    9. plugin.getCustomConfig().set("Town.Funds.DiamondLeggings.Current", 3);
    10. plugin.saveCustomConfig();
    11. plugin.reloadCustomConfig();
    12. TreeMap<String, Integer> map = new TreeMap<String, Integer>();
    13. map.put("Thunder_Waffe", plugin.getCustomConfig().getInt("Town.Funds.Thunder_Waffe.Current"));
    14. map.put("Creeper_Fever", plugin.getCustomConfig().getInt("Town.Funds.Creeper_Fever.Current"));
    15. map.put("DiamondLeggings", plugin.getCustomConfig().getInt("Town.Funds.DiamondLeggings.Current"));
    16. map.put("Zedr", 6);
    17. map.put("Abba", 4);
    18. map.put("Aaba", 5);
    19. NavigableMap<String, Integer> descMap = map.descendingMap();
    20. Set<String> keySet = descMap.keySet();
    21. for(String key : keySet){
    22. int value = descMap.get(key);
    23. System.out.println(key + " - " + value);
    24. }
    25. }
    26. }
    27.  


    Prints out:

    Zedr - 6
    Thunder_Waffe - 1
    DiamondLeggings - 3
    Creeper_Fever - 2
    Abba - 4
    Aaba - 5
     
Thread Status:
Not open for further replies.

Share This Page