Skip to content

Instantly share code, notes, and snippets.

@oshiro-kazuma
Created June 22, 2012 11:13
Show Gist options
  • Save oshiro-kazuma/2972140 to your computer and use it in GitHub Desktop.
Save oshiro-kazuma/2972140 to your computer and use it in GitHub Desktop.
Java標準クラスでソート
// List<HogeBean> beans = Hoge.findAll();
Collections.sort(beans, new Comparator<HogeBean>() {
@Override
public int compare(HogeBean o1, HogeBean o2) {
int comp = compareString(o1.getKey(), o2.geKey());
if (comp != 0) {
return comp;
}
return 0;
}
});
private int compareString(String s1, String s2) {
if (s1 == null && s2 == null) {
return 0;
} else if (s1 == null) {
return -1;
} else if (s2 == null) {
return 1;
} else {
return s1.compareTo(s2);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment