【发布时间】:2014-05-16 10:53:27
【问题描述】:
我在类约会对象的数组列表上调用 collections.sort,但它不会对任何事情进行排序
这就是我调用集合排序的方式
ArrayList<appointment> listoffappointments = new ArrayList<appointment>();
//then everytime on button click user adds a new appointment after setting the following code is executed
{
Calendar calendar1 = Calendar.getInstance();
calendar1.set(mYear, mMonth, mDay, temphour, tempmin);
appointment tempppt = new appointment(c2,tempstring1);
listoffappointments.add(tempppt);
Collections.sort(listoffappointments);
}
这是课程,有人可以找到问题,谢谢
import java.util.Calendar;
public class appointment implements Comparable<appointment> {
Calendar time;
String Text;
public appointment(Calendar tempc, String temptext) {
Text = temptext;
time = tempc;
}
public void settext(String text)
{
Text = text;
}
public String gettext()
{
return Text;
}
public String toString() {
return Text;
}
public Long gettimeofappt()
{
return time.getTimeInMillis();
}
@Override
public int compareTo(appointment o) {
return (this.gettimeofappt() < o.gettimeofappt() ) ? -1: (this.gettimeofappt() > o.gettimeofappt() ) ? 1:0 ;
}
}
【问题讨论】:
-
您的 collections.sort 调用在哪里?
-
在我的另一个类中具有类约会对象的数组列表
-
您应该在问题中包含该代码。
-
现在将其添加到问题中@nKn
-
假设我的数组列表中已经有约会类型的元素
标签: android sorting arraylist comparable compareto