【发布时间】:2013-01-25 10:15:24
【问题描述】:
我有课(JavaBean,如果你想这样称呼它)
class Tweet{
private millis; //number of millis since 1970
//other attributes and getters and setters, but i want to sort onlny by millis
public long getMillis() {
return millis;
}
}
Comparator 应该看起来与此类似:
class TweetComparator implements Comparator {
@Override
public int compare(Tweet t1, Tweet t2) {
//something
//this doesn't work
//return t2.getMillis().compareTo(t1.getMillis());
return ??;//what should be here?
}
}
这将在程序中
List<Tweet> tweets = new ArrayList<Tweet>();
tweets.add(...); //just fill the list
//i need newest (with hightest millis value first) so I probably need to call reverse order
Collection.reverse(tweets)
Collection.sort(tweets, new TweetComparator());
【问题讨论】:
-
当您尝试注释掉的代码时发生了什么?
-
@RohitJain 写入错误:无法在原始类型 long 上进行 compareTo(long)
标签: java sorting collections compare long-integer