Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class DateCompare {
- public final Integer day;
- public final Integer month;
- public final Integer year;
- public DateCompare(String date) {
- String[] components = date.split("\\.");
- day = Integer.valueOf(components[0]);
- month = Integer.valueOf(components[1]);
- year = Integer.valueOf(components[2]);
- }
- @Override
- public String toString() {
- return day + "." + month + "." + year + ".";
- }
- }
- public class DomaciZadatakZaMozganje1 {
- public static void main(String[] args) {
- DateCompare[] dates = new DateCompare[] {
- new DateCompare("11.4.2015."),
- new DateCompare("12.5.2013."),
- new DateCompare("21.5.2015."),
- new DateCompare("11.4.2014."),
- new DateCompare("14.4.2015."),
- new DateCompare("14.4.2014."),
- new DateCompare("21.4.2015."),
- new DateCompare("12.5.2014."),
- new DateCompare("11.4.2013."),
- new DateCompare("12.5.2015."),
- new DateCompare("14.4.2013."),
- new DateCompare("16.5.2015.")
- };
- Svetovid.out.println("Unsorted :");
- for (DateCompare date : dates) {
- Svetovid.out.println(date);
- }
- Arrays.sort(dates, new DateComparator());
- Svetovid.out.println("Sorted: ");
- for (DateCompare date : dates) {
- Svetovid.out.println(date);
- }
- }
- }
- class DateComparator implements Comparator<DateCompare> {
- private Comparator<Integer> comparator = new ReverseNumComparator();
- @Override
- public int compare(DateCompare DateCompare1, DateCompare DateCompare2) {
- int result = comparator.compare(DateCompare1.year, DateCompare2.year);
- if (result == 0)
- result = comparator.compare(DateCompare1.month, DateCompare2.month);
- if (result == 0)
- result = comparator.compare(DateCompare1.day, DateCompare2.day);
- return result;
- }
- }
- class ReverseNumComparator implements Comparator<Integer> {
- @Override
- public int compare(Integer int1, Integer int2) {
- if (int1 == int2) {
- return 0;
- } else if (int1 < int2) {
- return 1;
- } else {
- return -1;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement