2014-04-04 27 views
0

林调用collections.sort ArrayList类任用对象的数组列表上,但它不会排序没有任何反应排序android系统不工作

这是IM调用集合排序

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 ; 

} 


} 
+1

哪里是你collections.sort电话吗? – Sree

+0

在我的其他类中有类约会对象的数组列表 – theForgottenCoder

+0

您应该在您的问题中包含该代码。 – nKn

回答

2

你是返回一个Long对象,更改您的compareTo,如:

@Override 
public int compareTo(appointment o) { 
    return this.gettimeofappt().compareTo(o.gettimeofappt()); 

} 

Long.compareTo比较两个Long对象数值,和Retunrs:

值0,如果这是长等于参数龙;如果此Long在数字上小于参数Long,则值小于 ;和一个 值大于0,如果此长数字上大于 参数长(有符号比较)。

+0

来比较,即使使用“return this.gettimeofappt()。compareTo(o.gettimeofappt());”没有什么变化@blackbelt – theForgottenCoder

+0

你可以打印ArrayList的内容吗?只是为了调试puropse – Blackbelt

+0

数组列表的内容完全按照它们在第一个然后第一个第二个第一个第二个第三个 – theForgottenCoder