Sort an array of dates based on the current date

I have an array:

|YYYY-MM-DD||YYYY-MM-DD||YYYY-MM-DD||...||....||....

Now I query the system for the current date and based on this date, I want my array to be sorted, the year is irrelevant the month and day are important. I want the closest date at index 0 and the farthest date last.

Say for example there are three dates 1999-04-04, 1789-03-01, 2012-05-04

if it is the month of April: The sorted array should be like

1999-04-04, 2012-05-04, 1789-03-01.

I am looking for logic, not asking for doing my homework. I read a lot about the subject but I am not able to devise a path. Help would be highly appreciated.

I want my array to be sorted, the year is irrelevant the month and day are important. I want the closest date at index 0 and the farthest date last.

You will need to sort based upon the month and day first: While comparing any two Dateঃ

First sort the Date list:

1. Compare the month and if they are not equal return the difference(month1 - month2) as the compared result
2. If the month are equal return the difference of day of the month as the compared result

So compareTo(Date o) function of an implemented Comparable<Date> would look like:

```    @Override
public int compareTo(Date o) {
Calendar cal1 = Calendar.getInstance();
cal1.setTime(this.date);
Calendar cal2 = Calendar.getInstance();
cal2.setTime(o);

int month1 = cal1.get(Calendar.MONTH);
int month2 = cal2.get(Calendar.MONTH);

if(month1 < month2)
return -1;
else if(month1 == month2)
return cal1.get(Calendar.DAY_OF_MONTH) - cal2.get(Calendar.DAY_OF_MONTH);

else return 1;

}
```

After sorting you just could round the list, thinking it is circular. For example suppose the sorted list (excluding the year as it is irrelevant):

```JAN 20, FEB 5, SEP 18, OCT 9, OCT 20, NOV 23
```

If our pivot(the closes date comparing to the date) is OCT 11 choosing the immediate larger(smallest date larger than pivot) date to it, would be OCT 20. You can find it just using a for loop. Now, we just need to round it thinking it is circular:

```OCT 20, NOV 23 --> JAN 20, FEB 5, SEP 18, OCT 9
```

Formally, find the index i of immediate larger date comparing to our pivot based upon month and day(try using the compareTo example), then create a new list, insert the element starting from the index i to n-1 and then 0 to i-1, here n is the size of the Date list.

You definitely want to implement a java.util.Comparator and pass it to your sort method. The code in it should actually compare the absolute difference of number of days between candidate and current date.

Implement a java.util.Comparator. The comparator can take a date in the constructor and store it as reference date in an attribute. The compare() method can then decide which one of the passed dates is closer to the reference date (according to whatever definition of close you prefer).

The comperator can then be passed along with the array to Array.sort(T[] a, Comparator<? super T> c)

it is actually simplier to code than to explain:

```Arrays.sort(array, new Comparator<Date>()
{
Calendar now = Calendar.getInstance();

@Override
public int compare(Date d1, Date d2)
{
Calendar c1 = Calendar.getInstance();
c1.setTime(d1);
c1.set(Calendar.YEAR, now.get(Calendar.YEAR)); // year is irrilevant for d1

Calendar c2 = Calendar.getInstance();
c2.setTime(d2);
c2.set(Calendar.YEAR, now.get(Calendar.YEAR)); // year is irrilevant for d2

Long distance1 = Long.MAX_VALUE;
Long distance2 = Long.MAX_VALUE;

for(int i : new Integer[] { -1, 0, 1 })
{
c1.set(Calendar.YEAR, now.get(Calendar.YEAR) + i);
c2.set(Calendar.YEAR, now.get(Calendar.YEAR) + i);

Long temp1 = Math.abs(c1.getTimeInMillis() - now.getTimeInMillis());
Long temp2 = Math.abs(c2.getTimeInMillis() - now.getTimeInMillis());

distance1 = Math.min(distance1, temp1);
distance2 = Math.min(distance2, temp2);
}

return distance1.compareTo(distance2);
}
});
```