因此,使用TreeMap,我能够按键(日期)对HashMap进行排序,但接下来要做的是,如果日期相同,那么我想按值(时间)排序。有人可以建议如何完成这项工作吗?
以下是按键正确排序的相关代码:
public void eventList(){
int year = -1;
if(MyCalendarTester.myCal.getMyCalHash().equals(null)){
System.out.println("Your calendar is empty!");
}
else{
System.out.println("Here are your events: ");
SortedSet<GregorianCalendar> keys = new TreeSet<GregorianCalendar>(MyCalendarTester.myCal.getMyCalHash().keySet());
for(GregorianCalendar key : keys){
Event value = MyCalendarTester.myCal.getMyCalHash().get(key);
if(value.endTime != null){
if(key.get(Calendar.YEAR) == year){
System.out.println(MyCalendarTester.arrayOfDays[key.get(Calendar.DAY_OF_WEEK) - 1] + ", " + MyCalendarTester.arrayOfMonths[key.get(Calendar.MONTH) - 1] + " "
+ key.get(Calendar.DATE) + " " + value.startTime.get(Calendar.HOUR_OF_DAY) + ":" + value.startTime.get(Calendar.MINUTE) + " - " + value.endTime.get(Calendar.HOUR_OF_DAY)
+ ":" + value.endTime.get(Calendar.MINUTE) + " " + value.eventName);
}else{
System.out.println(key.get(Calendar.YEAR));
System.out.println(MyCalendarTester.arrayOfDays[key.get(Calendar.DAY_OF_WEEK) - 1] + ", " + MyCalendarTester.arrayOfMonths[key.get(Calendar.MONTH) - 1] + " "
+ key.get(Calendar.DATE) + " " + value.startTime.get(Calendar.HOUR_OF_DAY) + ":" + value.startTime.get(Calendar.MINUTE) + " - " + value.endTime.get(Calendar.HOUR_OF_DAY)
+ ":" + value.endTime.get(Calendar.MINUTE) + " " + value.eventName );
year = key.get(Calendar.YEAR);
}
}else{
if(key.get(Calendar.YEAR) == year){
System.out.println(MyCalendarTester.arrayOfDays[key.get(Calendar.DAY_OF_WEEK) - 1] + ", " + MyCalendarTester.arrayOfMonths[key.get(Calendar.MONTH) - 1] + " "
+ key.get(Calendar.DATE) + " " + value.startTime.get(Calendar.HOUR_OF_DAY) + ":" + value.startTime.get(Calendar.MINUTE) + " " + value.eventName);
}else{
System.out.println(key.get(Calendar.YEAR));
System.out.println(MyCalendarTester.arrayOfDays[key.get(Calendar.DAY_OF_WEEK) - 1] + ", " + MyCalendarTester.arrayOfMonths[key.get(Calendar.MONTH) - 1] + " "
+ key.get(Calendar.DATE) + " " + value.startTime.get(Calendar.HOUR_OF_DAY) + ":" + value.startTime.get(Calendar.MINUTE) + " " + value.eventName);
System.out.println();
year = key.get(Calendar.YEAR);
}
}
}
}
}
答案 0 :(得分:2)
您需要创建一个嵌入键的两个组件的自定义Comparator
(date, time).
这样,您的数据会先按日期排序,然后按时间排序。
你可以通过将(日期,时间)组合成一个元组,然后在比较器中使用元组compareTo来实现这一点。
<强>更新强>
另一个选项是,如果您不希望更改键条目(这是btw map / reduce中的常见模式以进行二级排序..):使您的值条目成为已排序的集合并迭代地比较集合条目。
比较可以通过
完成merge()
mergesort的逻辑:两个列表需要单独降序排序。然后开始合并列表。这里的区别在于,只要找到任何差异,就会返回包含给定条目的列表
答案 1 :(得分:0)
尝试使用此代码,根据日期时间
对所有Event对象进行排序//* add a comparable to your event class
public class Event implements Comparable
{
Calendar endTime = .....
.....................
public int compareTo(Object o)
{
Event event = (Event)o;
long eventTime = event.endTime.getTimeInMillis();
long time = endTime.getTimeInMillis();
return (int)(time - eventTime);
}
}
//* loop over all of your event objects and add the correct date
for(GregorianCalendar key : keys)
{
Event value = MyCalendarTester.myCal.getMyCalHash().get(key);
value.endTime.set(Calendar.YEAR, key.get(Calendar.YEAR));
value.endTime.set(Calendar.MONTH, key.get(Calendar.MONTH));
value.endTime.set(Calendar.DAY_OF_MONTH, key.get(DAY_OF_MONTH));
}
//* get the array of all events and sort them
Event[] events = (Event[])MyCalendarTester.myCal.getMyCalHash().values().toArray(new Event[0]);
Arrays.sort(events);