forked from rhyzue/KettleLog
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathOptionComparators.java
86 lines (69 loc) · 2.59 KB
/
OptionComparators.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import Item.*;
import java.time.*;
import java.util.*;
import javafx.util.*;
import javafx.event.*;
import javafx.collections.*;
public class OptionComparators extends Kettlelog{
private static ObservableList<Item> optionData;
private Kettlelog kettle = new Kettlelog();
public OptionComparators(){
optionData = kettle.getData();
}
//Methods
public void sortByMostRecent(int order){
DateComparator comp = new DateComparator();
//sort by most recent
if(order==2){
optionData.sort(comp.reversed());
}
//sort by oldest
else if (order==3){
optionData.sort(comp);
}
kettle.setData(optionData, 3); //in kettle, update data value
}
public void sortByStarred(){
StarComparator comp = new StarComparator();
optionData.sort(comp.reversed());
kettle.setData(optionData, 3);
}
//Classes
public class DateComparator implements Comparator<Item>{
@Override
public int compare(Item c1, Item c2){
String c1Date = c1.getDateAdded();
String c2Date = c2.getDateAdded();
//Split date string into an array: year, month, day
String[] c1Split = c1Date.split("-");
String[] c2Split = c2Date.split("-");
int[] c1Dates = new int[3];
int[] c2Dates = new int[3];
//Convert String dates to integer
for(int i = 0; i<3; i++){
try {
c1Dates[i] = Integer.parseInt(c1Split[i]);
c2Dates[i] = Integer.parseInt(c2Split[i]);
}
catch (NumberFormatException e){
c1Dates[i] = 0;
c2Dates[i] = 0;
System.out.println("ERROR");
}
//Compare each element in the array only if they are different
//ie. if yr1==yr2, then don't return and instead compare the next element
if (c1Dates[i] != c2Dates[i]){
return Integer.valueOf(c1Dates[i]).compareTo(Integer.valueOf(c2Dates[i]));
}
}
//if the for loop has not returned anything, 2 dates are the same. return any random comparison
return Integer.valueOf(c1Dates[0]).compareTo(Integer.valueOf(c2Dates[0]));
}
}
public class StarComparator implements Comparator<Item> {
@Override
public int compare(Item c1, Item c2) {
return Boolean.valueOf(c1.getStarred()).compareTo(Boolean.valueOf(c2.getStarred()));
}
}
}