私は配列と列挙型をいじっていますが、 Comparator クラスを作成してこれらの日付を降順でソートする最も効果的な方法は何だろうと思っていました。これが私のコードです。
public enum Month {JAN(1), FEB(2), MAR(3), APR(4), MAY(5), JUN(6), JUL(7),
AUG(8), SEPT(9), OCT(10), NOV(11), DEC(12);
final int monthBoundary;
Month(int y){
monthBoundary=y;}
}
public enum Date {FIRST(1), SECOND(2), THIRD(3), FORTH(4),
FIFTH(5)... THIRTYFIRST(31);
final int dateBoundary;
Date(int z){
dateBoundary=z;}
}
//constructor etc here
private static final List<Cal> calendar = new ArrayList<Cal>();
static {
for (Month month : Month.values()) {
for (Date date : Date.values()) {
calendar.add(new Cal(date, month));
}
}
}
//creates new calendar dates
public static ArrayList<Cal> newCal() {
return new ArrayList<Cal>(calendar);
}
次のステートメントを使用すると、作成された順序で配列を出力できます。
System.out.print(Card.calendar());
これらの日付を降順に並べ替える Comparator クラスをどのように作成しますか? 理想的には、配列がすでに順序付けられているかランダムな順序であるかにかかわらず、配列を並べ替えたいと思います。この段階では、存在しない日付 (たとえば 2 月 31 日) については心配していません。ただ練習して独学しているだけなので... 概念を理解しようとしているだけです :) ありがとう。
追加した:
public ArrayList<Cal> sortDescending(ArrayList<Cal> calList){
Comparator<Cal> c = Collections.reverseOrder();
Collections.sort(calList, c);
return calList;
}