Created
June 22, 2012 11:13
-
-
Save oshiro-kazuma/2972140 to your computer and use it in GitHub Desktop.
Java標準クラスでソート
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
// 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