英文:
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()+" - Drops");
j++;
player.getPacketSender().sendInterface(37600);
for (int i = 0; i < 70 - j; i++) {
player.getPacketSender().sendString(ITEM_STRING+i, "");
player.getPacketSender().sendString(AMOUNT_STRING+i, "");
player.getPacketSender().sendString(RARITY_STRING+i, "");
}
List<Item> drop = new ArrayList<>();
int index = 0;
List<NPCDrops.NpcDropItem> 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 > 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()+"");
}
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());
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论