Skip to content

Instantly share code, notes, and snippets.

@shisama
Last active July 5, 2018 04:05
Show Gist options
  • Save shisama/2c168c0a6017e65868d1 to your computer and use it in GitHub Desktop.
Save shisama/2c168c0a6017e65868d1 to your computer and use it in GitHub Desktop.
Java8 ラムダ式でComparatorのコールバック関数を簡単に書く方法 ref: https://qiita.com/shisama/items/1ba7e4f0000d4e7a9b5e
Collections.sort(list, new Comparator<Person>(){
@Override
public int compare(Person p1, Person p2) {
int ret = p1.getId() - p2.getId();
if (ret == 0) {
ret = p1.getName().compareTo(p2.getName());
}
if (ret == 0) {
ret = p1.getAge() - p2.getAge();
}
return ret;
}
});
Collections.sort(list, (p1, p2) -> {
int ret = Integer.compare(p1.getId(), p2.getId());
if (ret == 0) {
ret = p1.getName().compareTo(p2.getName();
}
if (ret == 0) {
ret = Integer.compare(p1.getAge(), p2.getAge());
}
return ret;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment