I have an array:
我有一个数组:
|YYYY-MM-DD||YYYY-MM-DD||YYYY-MM-DD||...||....||....
| 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.
现在我查询系统的当前日期,基于这个日期,我希望我的数组被排序,年份不相关,月和日很重要。我想要索引0处最近的日期和最远的日期。
Say for example there are three dates 1999-04-04, 1789-03-01, 2012-05-04
例如,有三个日期: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.
1999-04-04,2012-05-04,1999-04-04。
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.
我在寻找逻辑,而不是要求做作业。我读了很多关于这个主题的书,但我不能设计出一条路来。非常感谢您的帮助。
4 个解决方案
#1
1
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.
我想要我的数组排序,年份无关,月份和日期很重要。我想要索引0处最近的日期和最远的日期。
You will need to sort based upon the month and day first: While comparing any two Dateঃ
你需要根据月第一天:ঃ虽然比较任意两个日期
First sort the Date list:
首先对日期列表进行排序:
- Compare the month and if they are not equal return the difference(month1 - month2) as the compared result
- 比较月份,如果它们不相等则返回差异(月1 -月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:
如果我们的pivot(与日期比较的关闭日期)是10月11日,选择它的当前较大的日期(比pivot日期大的最小日期),那么就是10月20日。你可以用for循环来找到它。现在,我们只需把它四舍五入,认为它是圆的:
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.
正式,找到直接的指标我大日期比较基于月和日主(尝试使用compareTo例子),然后创建一个新的列表,插入元素从指数n - 1和0张,这里n是日期列表的大小。
#2
1
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.
您肯定希望实现java.util。比较器并将其传递给排序方法。其中的代码实际上应该比较候选日期和当前日期之间的天数的绝对差异。
#3
1
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).
实现一个java.util.Comparator。比较器可以在构造函数中使用日期,并将其存储为属性中的引用日期。然后,compare()方法可以决定哪个已传递的日期更接近引用日期(根据您喜欢的任何close定义)。
The comperator can then be passed along with the array to Array.sort(T[] a, Comparator<? super T> c)
然后可以将comperator与数组一起传递给数组。排序(T[]一个Comparator < ?超级T > c)
#4
1
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);
}
});
#1
1
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.
我想要我的数组排序,年份无关,月份和日期很重要。我想要索引0处最近的日期和最远的日期。
You will need to sort based upon the month and day first: While comparing any two Dateঃ
你需要根据月第一天:ঃ虽然比较任意两个日期
First sort the Date list:
首先对日期列表进行排序:
- Compare the month and if they are not equal return the difference(month1 - month2) as the compared result
- 比较月份,如果它们不相等则返回差异(月1 -月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:
如果我们的pivot(与日期比较的关闭日期)是10月11日,选择它的当前较大的日期(比pivot日期大的最小日期),那么就是10月20日。你可以用for循环来找到它。现在,我们只需把它四舍五入,认为它是圆的:
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.
正式,找到直接的指标我大日期比较基于月和日主(尝试使用compareTo例子),然后创建一个新的列表,插入元素从指数n - 1和0张,这里n是日期列表的大小。
#2
1
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.
您肯定希望实现java.util。比较器并将其传递给排序方法。其中的代码实际上应该比较候选日期和当前日期之间的天数的绝对差异。
#3
1
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).
实现一个java.util.Comparator。比较器可以在构造函数中使用日期,并将其存储为属性中的引用日期。然后,compare()方法可以决定哪个已传递的日期更接近引用日期(根据您喜欢的任何close定义)。
The comperator can then be passed along with the array to Array.sort(T[] a, Comparator<? super T> c)
然后可以将comperator与数组一起传递给数组。排序(T[]一个Comparator < ?超级T > c)
#4
1
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);
}
});