Collections.sort()
无法正常工作。当我将Collections.reverse()
放在Collections.sort()
之前时,ArrayList
的顺序不同。这个怎么可能?是相同的数据,只是还原了。
messages = new ArrayList<Message>();
messageDatabase.getConditionBuilder().setSortOrder(DatabaseHelper.KEY_MESSAGE_TIME + " DESC");
messageDatabase.getConditionBuilder().setSqlLimit(100);
messages.addAll(messageDatabase.getList());
// different results if I remove the reverse line
Collections.reverse(messages);
Collections.sort(messages);
messageAdapter = new MessageAdapter(this, R.layout.list_message_item, messages);
messageAdapter.setIsPrivateChat(true);
listView.setAdapter(messageAdapter);
我的消息模型实现了可比性
@Override
public int compareTo(Message another) {
return Float.compare(time, another.getTime());
}
更新:
我已经更新了一些代码并添加了调试功能。现在按localTime排序,这是unix时间戳(以微秒为单位)。没有相等的时间戳记。
@Override
public int compareTo(Message another) {
return Float.compare(localTime, another.getLocalTime());
}
从数据库读取:
messages = new ArrayList<Message>();
messageDatabase.getConditionBuilder().setSortOrder(DatabaseHelper.KEY_MESSAGE_LOCAL_TIME + " DESC");
messageDatabase.getConditionBuilder().setSqlLimit(100);
messages.addAll(messageDatabase.getList());
for (Message m : messages) {
Log.i("debug", "time unsorted: " + m.getLocalTime());
}
Collections.sort(messages);
for (Message m : messages) {
Log.i("debug", "time: " + m.getLocalTime());
}
结果(缩短并削减前6个相等的数字):
06-21 15:56:14.734: I/debug(13886): time unsorted: 4607969
06-21 15:56:14.734: I/debug(13886): time unsorted: 4607303
06-21 15:56:14.734: I/debug(13886): time unsorted: 4591162
06-21 15:56:14.734: I/debug(13886): time unsorted: 3372601
06-21 15:56:14.734: I/debug(13886): time unsorted: 3338542
06-21 15:56:14.734: I/debug(13886): time unsorted: 3338514
06-21 15:56:14.734: I/debug(13886): time unsorted: 3338481
06-21 15:56:14.734: I/debug(13886): time unsorted: 3338455
06-21 15:56:14.734: I/debug(13886): time unsorted: 3338415
06-21 15:56:14.734: I/debug(13886): time unsorted: 3338375
06-21 15:56:14.734: I/debug(13886): time unsorted: 3338339
06-21 15:56:14.734: I/debug(13886): time unsorted: 3338244
06-21 15:56:14.734: I/debug(13886): time: 3338542
06-21 15:56:14.734: I/debug(13886): time: 3338514
06-21 15:56:14.734: I/debug(13886): time: 3338481
06-21 15:56:14.734: I/debug(13886): time: 3338455
06-21 15:56:14.734: I/debug(13886): time: 3338415
06-21 15:56:14.734: I/debug(13886): time: 3338375
06-21 15:56:14.734: I/debug(13886): time: 3338339
06-21 15:56:14.734: I/debug(13886): time: 3338244
06-21 15:56:14.734: I/debug(13886): time: 3372601
06-21 15:56:14.734: I/debug(13886): time: 4607969
06-21 15:56:14.734: I/debug(13886): time: 4607303
06-21 15:56:14.734: I/debug(13886): time: 4591162
但实际上我希望排序后的数组如下所示:
06-21 15:56:14.734: I/debug(13886): time: 3338244
06-21 15:56:14.734: I/debug(13886): time: 3338339
06-21 15:56:14.734: I/debug(13886): time: 3338375
06-21 15:56:14.734: I/debug(13886): time: 3338415
06-21 15:56:14.734: I/debug(13886): time: 3338455
06-21 15:56:14.734: I/debug(13886): time: 3338481
06-21 15:56:14.734: I/debug(13886): time: 3338514
06-21 15:56:14.734: I/debug(13886): time: 3338542
06-21 15:56:14.734: I/debug(13886): time: 3372601
06-21 15:56:14.734: I/debug(13886): time: 4591162
06-21 15:56:14.734: I/debug(13886): time: 4607303
06-21 15:56:14.734: I/debug(13886): time: 4607969
我是否误解了
Collections.sort()
? 最佳答案
Long.compare(long, long)
应该用于比较长值。
What's wrong with using == to compare floats in Java?
考虑以下项目A-F的集合,每个项目都有一个整数属性,大小。
A(1)
B(1)
C(2)
D(4)
E(5)
F(2)
Collections.reverse()将执行以下操作:
F(2)
E(5)
D(4)
C(2)
B(1)
A(1)
然后Collections.sort()会根据大小对商品重新排序,但其他顺序保持不变:
E(5)
D(4)
F(2)
C(2)
B(1)
A(1)
这样,如果您只执行Collections.sort()而没有先反转集合,A&B和C&F将保持相同的顺序:
E(5)
D(4)
C(2)
F(2)
A(1)
B(1)
关于java - Collections.sort无法与ListView和Adapter一起正常工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30963081/