如何使 Java Int Array 以降序显示

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

How to make Java Int Array show in Desending order

问题

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

for(NPCDrops.NpcDropItem item : dropList) {
    ItemDefinition itemDef = ItemDefinition.forId(item.getId());
    if(index > 69 || itemDef == null|| itemDef.getName().equalsIgnoreCase("none"))
        continue;

    drop.add(item.getItem());
    index++;
    player.getPacketSender().sendString(ITEM_STRING+index, itemDef.getName());
    player.getPacketSender().sendString(AMOUNT_STRING+index, item.getCount()[0]+"");
    player.getPacketSender().sendString(RARITY_STRING+index, item.getChance().getRandom() == 0 ? "Always" : "1/"+item.getChance().getRandom()+"");
}

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

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 -->

public static void display(Player player, NPCDrops definition) {
    	  int j = 0;
        player.getPacketSender().sendFrame126(37602, NpcDefinition.forId(definition.getNpcIds()[0]).getName()+&quot; - Drops&quot;);
        j++;
        player.getPacketSender().sendInterface(37600);
      
        for (int i = 0; i &lt; 70 - j; i++) {
            player.getPacketSender().sendString(ITEM_STRING+i, &quot;&quot;);
            player.getPacketSender().sendString(AMOUNT_STRING+i, &quot;&quot;);
            player.getPacketSender().sendString(RARITY_STRING+i, &quot;&quot;);
        }
        List&lt;Item&gt; drop = new ArrayList&lt;&gt;();
        int index = 0;
        
        List&lt;NPCDrops.NpcDropItem&gt; dropList = Arrays.asList(definition.getDropList());

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

        for(NPCDrops.NpcDropItem item : dropList) {
            ItemDefinition itemDef = ItemDefinition.forId(item.getId());
            if(index &gt; 69 || itemDef == null|| itemDef.getName().equalsIgnoreCase(&quot;none&quot;))
                continue;

            drop.add(item.getItem());
            index++;
            player.getPacketSender().sendString(ITEM_STRING+index, itemDef.getName());
            player.getPacketSender().sendString(AMOUNT_STRING+index, item.getCount()[0]+&quot;&quot;);
            player.getPacketSender().sendString(RARITY_STRING+index, item.getChance().getRandom() == 0 ? &quot;Always&quot; : &quot;1/&quot;+item.getChance().getRandom()+&quot;&quot;);

        }
        player.getPacketSender().sendItemsOnInterface(37915, 70, drop, true);

    }
}

答案1

得分: 1

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

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

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

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

You sort your objects with a comparator.

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

Luckily the interface does also provide a reversed method.

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:

确定