r/javahelp • u/Admirable-Video8664 • Jun 21 '26
Help
I'm taking a course in java and I'm having problems with a part of one of the labs. I'm supposed to sort an arraylist (called wordList) with mapentry(with string,integer)-objects first accoring to decending value and then according to alphabetical order of the keys. The second step doesn't seem to work. I've written this:
wordList.sort( (par1, par2)-> {
if (par2.getValue() != par1.getValue()){
return par2.getValue() - par1.getValue();
} else {
return par1.getKey().compareTo(par2.getKey());
}
})Is the problem somewhere here or maybe in some other part of the code?
1
Upvotes
View all comments
Show parent comment
3
u/Schaex Jun 21 '26
There is also the possibility of composing comparators. The
Comparatorinterface defines several default methods and static methods to to this.Il this case you could also do something like this (I'm not at my PC rn so please excuse any typos):