Sort a List for top 10

Discussion in 'Plugin Development' started by chriztopia, Nov 30, 2012.

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

    chriztopia

    very simple I want to take a yml list...

    Code:
    user: 0
    user: 2
    user: 0
    user: 5
    user: 0
    user: 1
    user: 6
    and Sort it as a top 10 List and display it back by command. I simply need to know how to pull the list and sort it and send it back to the player.
     
  2. So you want a list:
    Code:
    ListName:
    - Entry 1
    - Entry 2
    - Entry 3
    or just key/value mapping:
    Code:
    Entry1: A
    Entry2: B
    Entry3: C
    in your yaml file?
     
  3. Offline

    chriztopia

    I already have the lists I want to pull the data from the list and sort it. So if I done /top.
    Code:
    user: 0
    user: 2
    user: 0
    user: 5
    user: 0
    user: 1
    user: 6
    Would be displayed as
    user: 6
    user: 5
    user: 2
    user: 1
     
  4. I used something simulair to this, I had an list of winners in the same Map notation as your list is, and I needed the top 5 players of it, this is what I did:
    code (open)
    Code:java
    1. static void broadcastWinners(String world, String name, Map<String, Integer> winners)
    2. {
    3. if (winners.isEmpty())
    4. {
    5. FerryEggRoulettePlugin.broadcastMessage("This time, there where no winners at the egg roulette named '" + name + "'");
    6. return;
    7. }
    8. SortedSet<PlayerDataComparable> players = new TreeSet<PlayerDataComparable>();
    9. for (Map.Entry<String, Integer> i : winners.entrySet())
    10. {
    11. players.add(new PlayerDataComparable(i.getKey(), i.getValue()));
    12. economy.depositPlayer(i.getKey(), i.getValue().doubleValue());
    13. Player winner = Bukkit.getPlayerExact(i.getKey());
    14. if (winner != null)
    15. {
    16. sendMessage("You won, your money is now: " + moneyFormat(economy.getBalance(i.getKey())));
    17. }
    18. }
    19. Iterator<PlayerDataComparable> i = players.iterator();
    20. int counter = 0;
    21. while (i.hasNext() && counter < ChatPaginator.CLOSED_CHAT_PAGE_HEIGHT)
    22. {
    23. PlayerDataComparable next = i.next();
    24. broadcastMessage("Congrats " + next.getPlayer() + " for winning " + moneyFormat(next.getPrize()) + " at the place '" + name + "'");
    25. counter++;
    26. }
    27. if (i.hasNext())
    28. {
    29. broadcastMessage("Congrats 'more people' for winning at the place '" + name + "'");
    30. }
    31.  
    32. }
    Code:java
    1. /*
    2.  * To change this template, choose Tools | Templates
    3.  * and open the template in the editor.
    4.  */
    5. package me.ferry.bukkit.plugin.ferryeggroulette;
    6.  
    7. /**
    8.  *
    9.  * @author Fernando
    10.  */
    11. public final class PlayerDataComparable implements Comparable<PlayerDataComparable>, Cloneable, java.io.Serializable
    12. {
    13. private static final long serialVersionUID = 123456L;
    14. private final String player;
    15. private final int prize;
    16.  
    17. public PlayerDataComparable(String player, int prize)
    18. {
    19. this.player = player;
    20. this.prize = prize;
    21. }
    22.  
    23. @Override
    24. public int compareTo(PlayerDataComparable o)
    25. {
    26. if (o.prize > this.prize)
    27. {
    28. return -1;
    29. }
    30. if (o.prize < this.prize)
    31. {
    32. return 1;
    33. }
    34. return String.CASE_INSENSITIVE_ORDER.compare(this.player, o.player);
    35. }
    36.  
    37. @Override
    38. public boolean equals(Object obj)
    39. {
    40. if (obj == null)
    41. {
    42. return false;
    43. }
    44. if (getClass() != obj.getClass())
    45. {
    46. return false;
    47. }
    48. final PlayerDataComparable other = (PlayerDataComparable) obj;
    49. if ((this.player == null) ? (other.player != null) : !this.player.equals(other.player))
    50. {
    51. return false;
    52. }
    53. if (this.prize != other.prize)
    54. {
    55. return false;
    56. }
    57. return true;
    58. }
    59.  
    60. @Override
    61. public int hashCode()
    62. {
    63. return this.player.hashCode() ^ this.prize;
    64. }
    65.  
    66. @Override
    67. public String toString()
    68. {
    69. return "PlayerDataComparable{"
    70. + "player=" + player
    71. + ", prize=" + prize
    72. + '}';
    73. }
    74.  
    75. @Override
    76. public PlayerDataComparable clone() throws CloneNotSupportedException
    77. {
    78. return (PlayerDataComparable) super.clone();
    79. }
    80.  
    81. public String getPlayer()
    82. {
    83. return player;
    84. }
    85.  
    86. public int getPrize()
    87. {
    88. return prize;
    89. }
    90. }
    91.  
    I hope you under stand it and are able to rewrite it based on your list
    The following part of the code broadcasted the winnars in chat:
    Code:java
    1. Iterator<PlayerDataComparable> i = players.iterator();
    2. int counter = 0;
    3. while (i.hasNext() && counter < ChatPaginator.CLOSED_CHAT_PAGE_HEIGHT)
    4. {
    5. PlayerDataComparable next = i.next();
    6. broadcastMessage("Congrats " + next.getPlayer() + " for winning " + moneyFormat(next.getPrize()) + " at the place '" + name + "'");
    7. counter++;
    8. }
    9. if (i.hasNext())
    10. {
    11. broadcastMessage("Congrats 'more people' for winning at the place '" + name + "'");
    12. }
     
  5. What you show is a key/value mapping... So I'll give you hints about that:
    First read it in and save it to a HashMap. For that you need to loop though all keys and get its values:
    Code:java
    1. HashMap<String, Integer> map = new HashMap<String, Integer>();
    2. Configuration config = getConfig();
    3. for(String key: config.getKeys()) // loop over all keys...
    4. map.add(key, config.getInt(key)); //...and save them together with their value to the map.

    Last you need a TreeMap and a Comperator that holds the sorted result and glue it all together:
    Code:java
    1. TreeMap<String, Integer> sortedMap = new TreeMap<String, Integer>(Ordering.natural().onResultOf(Functions.forMap(map)).compound(Ordering.natural()));
    2. sortedMap.putAll(map);

    I hope I didn't forget anything as I didn't work with TreeMap/Comperators for some time...

    BTW: You probably want to do all this work in onEnable() and store the TreeMap globally for later access.
     
Thread Status:
Not open for further replies.

Share This Page