我不知道为什么我的列表没有排序。
我使用Collection.sort,它似乎可以完成这项工作,但是当我启动UnitTest时,它会输出错误。
预期:characterWithMaxVotes [voteCount:100]
实际:characterMiddle75 [voteCount:75]
//Exact same method as in the Character class (pasted for better readability on SO question)
public static void sortCharactersByVotes(List<Character> lstCharacters) {
Collections.sort(lstCharacters, new Comparator<Character>() {
@Override
public int compare(Character character, Character p1) {
int result = (character.getVoteCount() > p1.getVoteCount()) ? 1 : 0;
return result;
}
});
}
@Test
public void sortCharactersByVoteCounts() {
Character characterWithMinVotes = Character.newBuilder().name("characterWithMinVotes").voteCount(0).build();
Character characterMiddle25 = Character.newBuilder().name("characterMiddle25").voteCount(25).build();
Character characterMiddle75 = Character.newBuilder().name("characterMiddle75").voteCount(75).build();
Character characterWithMaxVotes = Character.newBuilder().name("characterWithMaxVotes").voteCount(100).build();
List<Character> lstCharacters = new ArrayList<>();
lstCharacters.add(characterMiddle75);
lstCharacters.add(characterWithMaxVotes );
lstCharacters.add(characterMiddle25);
lstCharacters.add(characterWithMinVotes);
sortCharactersByVotes(lstCharacters);
System.out.print(lstCharacters);
assertEquals(lstCharacters.get(0), characterWithMaxVotes);
assertEquals(lstCharacters.get(1), characterMiddle75);
assertEquals(lstCharacters.get(2), characterMiddle25);
assertEquals(lstCharacters.get(3), characterWithMinVotes);
}
如何正确地做到这一点,谢谢你的帮助。
PS:根据要求,这是我的角色类
公共类字符{
private static final String TAG = "Character";
private int id;
private String name = "";
public int voteCount;
public boolean isVotedByUser = false;
public int getId() {
return id;
}
public int getVoteCount() {
return voteCount;
}
public String getName() {
return name;
}
public static CharacterBuilder newBuilder(){
return new CharacterBuilder();
}
@Override
public String toString() {
return name + "[voteCount : " + voteCount + "]";
}
public static void sortCharactersByVotes(List<Character> lstCharacters) {
Collections.sort(lstCharacters, new Comparator<Character>() {
@Override
public int compare(Character character, Character p1) {
int result = (character.getVoteCount() > p1.getVoteCount()) ? 1 : 0;
return result;
}
});
}
public static class CharacterBuilder {
public Character character;
CharacterBuilder() {
character = new Character();
}
public CharacterBuilder id(int id) {
character.id = id;
return this;
}
public CharacterBuilder name(String name) {
character.name = name;
return this;
}
public CharacterBuilder voteCount(int voteCount) {
character.voteCount = voteCount;
return this;
}
public Character build() {
return character;
}
}
}
答案 0 :(得分:3)
(根据建议编辑使用Integer.compare
)
当且仅当两个比较的对象具有相等的&#34; rank&#34; (在您的情况下,等于投票数)时,compare
方法必须返回0,否则必须如果a> b则返回正值;如果b> a,则返回负值(反之亦然,根据所需的排序方向)。
要简单地解决这个问题,你可以写(假设getVoteCount()
是int
):
public int compare(Character character, Character p1) {
return Integer.compare(p1.getVoteCount(), character.getVoteCount());
}
(反转排序结果,只需交换操作数)
答案 1 :(得分:3)
Comparator
的实施方式不正确,因为您希望首先获得最高价值(而不是首先具有相反的最低值),如果character.getVoteCount() < p1.getVoteCount()
和character.getVoteCount() > p1.getVoteCount()
,您应该返回正值如果getVoteCount()
为负值,则应使用Integer.compare(int x, int y)
来比较int
的值(假设它返回Comparator
),以便new Comparator<Character>() {
@Override
public int compare(Character c1, Character c2) {
return Integer.compare(c2.getVoteCount(), c1.getVoteCount());
}
}
可以是:
getVoteCount()
NB:不要将window
的值与简单的减法进行比较,否则您将冒险获得不正确的结果,因为它容易出现溢出问题。
答案 2 :(得分:1)
我认为更好地改变比较器实现的一点点。尝试:
name
另一件事是你要加两次
<强> characterWithMaxVotes 强>
希望它有所帮助!
答案 3 :(得分:0)
您的比较方法应修改为
public static void sortCharactersByVotes(List<Character> lstCharacters) {
lstCharacters.sort(Comparator.comparingInt(Character::getVoteCount));
}
注意 - 比较基于getVoteCount
的整数Character
, Java 8 +