我有一個包含小時和分鐘的對象列表。該清單亂七八糟,我需要在00:00至23:59之間按小時排序。對日曆ArrayList排序
目的是
public class ProgramItem {
public int Hours;
public int Minutes;
public ProgramItem() {
}
public ProgramItem(int hours, int minutes, int power) {
Hours = hours;
Minutes = minutes;
}
public Calendar getCalendar() {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, Hours);
calendar.set(Calendar.MINUTE, Minutes);
return calendar;
}
}
我對它們進行排序的方法是
Collections.sort(items, new Comparator<ProgramItem>() {
public int compare(ProgramItem item1, ProgramItem item2) {
if (item1.getCalendar().before(item2.getCalendar())) {
return -1;
} else {
return 1;
}
}
})
例如:
輸入:02:00,09:00,15:00,21 :00,00:00,23:00
輸出應爲:00:00,01:00,02:00,09:00,15:00,21:00,23:00
輸出我有:02:00,09:00,15:00,21:00,23:00,00:00
的問題是,半夜總是在最後,但我需要它在一開始。
如何使排序從00:00開始到3:00-23:59結束? 感謝