Collections.sort()
is not working as expected. When I put Collections.reverse()
just before Collections.sort()
, the order of the ArrayList
is different. How can that be? It's the same data, just reverted.
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);
My Message model implements comparable
@Override
public int compareTo(Message another) {
return Float.compare(time, another.getTime());
}
Update: I've updated my code a little bit and added debugging. Ordering now by localTime, which is the unix timestamp in microseconds. There are no equal timestamps.
@Override
public int compareTo(Message another) {
return Float.compare(localTime, another.getLocalTime());
}
Reading from database:
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());
}
Result (shortened and cut the first 6 equal numbers):
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
But actually I expect the sorted array to look like this:
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
Did I missunderstood Collections.sort()
?