Collections.sort与listview和适配器无法正常工作

时间:2015-06-21 09:32:00

标签: java android

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()

2 个答案:

答案 0 :(得分:4)

Collections.sort使用稳定排序算法(修改后的合并排序),也就是说,如果某些项目具有对其进行排序的相等键,则在排序后它们的相互顺序将保持不变

这就是为什么在排序后反转您的收藏会更改排序。当然,只有具有相同排序键的项目才会有不同的顺序。

答案 1 :(得分:2)

应使用

Long.compare(long, long)来比较长值。

What's wrong with using == to compare floats in Java?

考虑以下项目集合A-F,每个项目都有一个整数属性 size

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&amp; B和C&amp; F将保持相同的顺序:

E(5)
D(4)
C(2)
F(2)
A(1)
B(1)