比较器没有正确排序

时间:2018-01-02 20:26:54

标签: java comparator

我试图对包含TurnChoices的ArrayList进行排序(我将展示此类的代码)。所以我创建了一个实现Comparator https://docs.oracle.com/javase/7/docs/api/java/util/Comparator.html

的类

比较方法应该返回-1,0,1,因为第一个参数小于,等于或大于第二个,对吧?我试图理解为什么我的ArrayList在第一个索引上具有较低的TurnChoice。我可以将-1替换为-1,将-1替换为1,但这不会尊重文档所说的内容,我想了解错误。

TurnChoiceComparater类:

package pkmnVerDemacia.battle.turn;

import java.util.Comparator;

import pkmnVerDemacia.battle.turn.item.UseItemChoice;
import pkmnVerDemacia.battle.turn.swap.SwapChampionChoice;
import pkmnVerDemacia.model.champion.Champion;
import pkmnVerDemacia.model.champion.stat.STAT;

public class TurnChoiceComparater implements Comparator<TurnChoice>
{

    @Override
    public int compare(TurnChoice tc1, TurnChoice tc2)
    {
        if (tc1.getPriority() > tc2.getPriority())
            return 1;
        else if (tc1.getPriority() == tc2.getPriority()) {
            return compareChampSpeed(tc1.getChampionUser(), tc2.getChampionUser());
        }
        else
            return this.getRandomSort();
    }

    private int compareChampSpeed(Champion c1, Champion c2) {
        if (c1.getStat(STAT.SPEED) > c2.getStat(STAT.SPEED)) {
            return 1;
        }
        else if (c1.getStat(STAT.SPEED) < c2.getStat(STAT.SPEED)) {
            System.out.println(c2.getName()+" is faster than "+c1.getName()+" so I return -1");
            System.out.println("first TurnChoice has Champion: "+c1.getName());
            System.out.println("second TurnChoice has Champion: "+c2.getName());
            return -1;
        }
        else
            return this.getRandomSort();
    }

    private int getRandomSort() {
        double random = Math.random();
        if (random > 0.5)
            return 1;
        else
            return -1;
    }
}

这里是TurnChoice类

package pkmnVerDemacia.battle.turn;

import pkmnVerDemacia.battle.BATTLE_PARTY;
import pkmnVerDemacia.battle.Battle;
import pkmnVerDemacia.battle.event.BattleEvent;
import pkmnVerDemacia.battle.event.BattleEventQueuer;
import pkmnVerDemacia.model.champion.Champion;

public abstract class TurnChoice
{
    protected BattleEventQueuer beQueuer;

    protected BATTLE_PARTY userParty;

    protected int priority;

    public TurnChoice(BattleEventQueuer beQueuer, BATTLE_PARTY userParty, int priority) {
        this.beQueuer = beQueuer;
        this.userParty = userParty;
        this.priority = priority;
    }

    public int getPriority()
    {
        return this.priority;
    }

    public BATTLE_PARTY getUserParty() {
        return this.userParty;
    }

    public BattleEventQueuer getBEQueuer() {
        return this.beQueuer;
    }

    public Champion getChampionUser() {
        return this.beQueuer.getChampion(this.userParty);
    }

    public abstract boolean begin();
}

这是我测试代码的地方:

public static void sortTurnChoices(ArrayList<TurnChoice> choices)
{
    Collections.sort(choices, new TurnChoiceComparater());
    for (TurnChoice choice : choices)
        System.out.println(choice.getChampionUser().getName());
}

此处的控制台结果:

enter image description here

正如你所看到的,Vi比Braum更快,所以为什么Braum是ArrayList的第一个索引?

0 个答案:

没有答案