I'm attempting to sort two strings using the collection.sort() method, but I'm having issues understanding the logic of the implementation. Here is what I have so far. Are there any issues with my implementation? Note: I want to sort them alphabetically: "Apple" > "Orange"
Collections.sort(mailbox.getMessages() , (String a, String b) -> {
if (String.valueOf(a.charAt(0)) > String.valueOf(b.charAt(0))) {
return -1;
}
else if (String.valueOf(a.charAt(0)) <
String.valueOf(b.charAt(0))) {
return 1;
}
else {
return 0;
}
});
aorbhas a possibility of beingnull. You should also only perform 1 comparison between characters and store that result.Collections.sort()method? That will sorts string lexicographically anyway. See docs.oracle.com/javase/7/docs/api/java/lang/…