如何使 Java Int Array 以降序显示

huangapple go评论90阅读模式
英文:

How to make Java Int Array show in Desending order

问题

这是您的显示代码,我将在其中突出显示需要修改的部分:

  1. for(NPCDrops.NpcDropItem item : dropList) {
  2. ItemDefinition itemDef = ItemDefinition.forId(item.getId());
  3. if(index > 69 || itemDef == null|| itemDef.getName().equalsIgnoreCase("none"))
  4. continue;
  5. drop.add(item.getItem());
  6. index++;
  7. player.getPacketSender().sendString(ITEM_STRING+index, itemDef.getName());
  8. player.getPacketSender().sendString(AMOUNT_STRING+index, item.getCount()[0]+"");
  9. player.getPacketSender().sendString(RARITY_STRING+index, item.getChance().getRandom() == 0 ? "Always" : "1/"+item.getChance().getRandom()+"");
  10. }

要按降序排列几率值,将dropList.sort(...)行更改为:

  1. dropList.sort(Comparator.comparing(NPCDrops.NpcDropItem::getChance).reversed());

这将使项目按几率值的降序排列,所以您会看到1/20的项目首先显示在列表中。

英文:

Right now this code shows in Ascending order of chance value, highest rarity showing first such as 1/2 and and lower chance rarity such as 1/20 is showing last on list . How can i make it show 1/20 chance/rarity in the beginning of the list?
for example, currentlty this list shows, 1/2, 1/3, 1/4, but i want it to show 1/4, 1/3, 1/2 ?

this is my display code

<!-- language: lang-java -->

  1. public static void display(Player player, NPCDrops definition) {
  2. int j = 0;
  3. player.getPacketSender().sendFrame126(37602, NpcDefinition.forId(definition.getNpcIds()[0]).getName()+&quot; - Drops&quot;);
  4. j++;
  5. player.getPacketSender().sendInterface(37600);
  6. for (int i = 0; i &lt; 70 - j; i++) {
  7. player.getPacketSender().sendString(ITEM_STRING+i, &quot;&quot;);
  8. player.getPacketSender().sendString(AMOUNT_STRING+i, &quot;&quot;);
  9. player.getPacketSender().sendString(RARITY_STRING+i, &quot;&quot;);
  10. }
  11. List&lt;Item&gt; drop = new ArrayList&lt;&gt;();
  12. int index = 0;
  13. List&lt;NPCDrops.NpcDropItem&gt; dropList = Arrays.asList(definition.getDropList());
  14. dropList.sort(Comparator.comparing(NPCDrops.NpcDropItem::getChance));
  15. for(NPCDrops.NpcDropItem item : dropList) {
  16. ItemDefinition itemDef = ItemDefinition.forId(item.getId());
  17. if(index &gt; 69 || itemDef == null|| itemDef.getName().equalsIgnoreCase(&quot;none&quot;))
  18. continue;
  19. drop.add(item.getItem());
  20. index++;
  21. player.getPacketSender().sendString(ITEM_STRING+index, itemDef.getName());
  22. player.getPacketSender().sendString(AMOUNT_STRING+index, item.getCount()[0]+&quot;&quot;);
  23. player.getPacketSender().sendString(RARITY_STRING+index, item.getChance().getRandom() == 0 ? &quot;Always&quot; : &quot;1/&quot;+item.getChance().getRandom()+&quot;&quot;);
  24. }
  25. player.getPacketSender().sendItemsOnInterface(37915, 70, drop, true);
  26. }
  27. }

答案1

得分: 1

你可以使用比较器对你的对象进行排序。

  1. dropList.sort(Comparator.comparing(NPCDrops.NpcDropItem::getChance));

幸运的是,接口还提供了一个反转的方法。

  1. dropList.sort(Comparator.comparing(NPCDrops.NpcDropItem::getChance).reversed());
英文:

You sort your objects with a comparator.

  1. dropList.sort(Comparator.comparing(NPCDrops.NpcDropItem::getChance));

Luckily the interface does also provide a reversed method.

  1. dropList.sort(Comparator.comparing(NPCDrops.NpcDropItem::getChance).reversed());

huangapple
  • 本文由 发表于 2020年9月28日 13:57:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/64096673.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定