java - Sorting method not sorting correctly -
i'm calling arrays.sort(schedule, c);
c instance of comparator so:
import java.util.comparator; public class firstocccomparator implements comparator<abstractevent> { public int compare(abstractevent event1, abstractevent event2) { int result = 0; if (event1 == null || event2 == null) { //system.out.println("null"); } else if (event1.hasmoreoccurrences() && event2.hasmoreoccurrences()) { result = event1.nextoccurrence().compareto(event2.nextoccurrence()); } return result; } }
the output i'm getting isn't it's supposed be. i'm wondering if can point me in right direction here. first sorting algorithm i've ever made , using concepts still new me (comparators , implementation), sorry multiple questions regarding code :)
edit difference between outputs: http://pastebin.com/lwy1jqkt
there 2 kinds of events, these hasmoreoccurrences() , nextoccurrence() methods:
dailyevent
public boolean hasmoreoccurrences() { boolean result = false; date check = nextoccurrence(); timescalled--; if (check instanceof date && check != null) { result = true; } return result; } public date nextoccurrence() { if (timescalled > recurrences) { return null; } else { calendar cal = calendar.getinstance(); cal.settime(starttime); cal.add(calendar.date, timescalled); timescalled++; return cal.gettime(); } }
weeklyevent
public boolean hasmoreoccurrences() { date tmp = nextoccurrence(); timescalled--; boolean result = false; if (tmp instanceof date && tmp != null) { result = true; } return result; } public date nextoccurrence() { calendar cal = calendar.getinstance(); cal.settime(starttime); cal.add(calendar.date, timescalled*7); if (cal.gettime().compareto(this.endtime) > 0) { return null; } else { timescalled++; return cal.gettime(); } }
there few things seem incorrect comparator.
for instance, happens if 1 of them null? how want sort? right considering 2 events equal if 1 of them null.
also, happens if 1 event has more occurences while other not? right comparisons on occurrences if both events have more occurrences. need handle case 1 has occurences while other not.
also, if occurence custom class, need evaluate comparator well.
Comments
Post a Comment