custom ArrayList에서 sort하기

Comparable를 implement한 뒤에, CompareTo 메소드를 오버라이딩한다.
삽입하는 값이랑, 기존의 값을 비교하여 1, -1을 return한뒤에,
Collections.sort(weeklyInfo); // 날짜가 낮은 것부터 다시 들어가도록 한다
그 다음에 이와 같이 sort를 사용하면 된다

public class WeeklyEvent implements Comparable<WeeklyEvent>{
 public String firstDayofWeek;
 SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy년 MM월 dd일");
 private String pluskey;
 private String minuskey;
 private int score;

 public WeeklyEvent(String firstDayofWeek, String pluskey, String minuskey, int score){
 this.firstDayofWeek = firstDayofWeek;
 this.pluskey = pluskey;
 this.minuskey = minuskey;
 this.score = score;

 public String getFirstDay(){
 return firstDayofWeek;
 public String getPluskey(){
 return pluskey;
 public String getMinuskey(){ return minuskey; }
 public int getScore(){
 return score;

 public int compareTo(@NonNull WeeklyEvent weeklyEvent) {
 String firstDay = ((WeeklyEvent) weeklyEvent).getFirstDay();
 Date date_ = null;
 Date date_ori = null;
 try {
 date_ = sdf2.parse(firstDay);
 date_ori = sdf2.parse(this.firstDayofWeek);
 } catch (ParseException e) {
 java.util.Calendar c_ = java.util.Calendar.getInstance();
 java.util.Calendar c_ori = java.util.Calendar.getInstance();
 Boolean result = c_.before(c_ori);
 //ascending order
 if(result == true){return 1;}else{return -1;}

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s