Last active
July 5, 2018 04:05
-
-
Save shisama/2c168c0a6017e65868d1 to your computer and use it in GitHub Desktop.
Java8 ラムダ式でComparatorのコールバック関数を簡単に書く方法 ref: https://qiita.com/shisama/items/1ba7e4f0000d4e7a9b5e
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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