简体   繁体   English

Collections.sort无法与ListView和Adapter一起正常工作

[英]Collections.sort not working as expected with listview and adapter

Collections.sort() is not working as expected. Collections.sort()无法正常工作。 When I put Collections.reverse() just before Collections.sort() , the order of the ArrayList is different. 当我把Collections.reverse()之前Collections.sort()的顺序ArrayList是不同的。 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. 现在按localTime排序,这是unix时间戳(以微秒为单位)。 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): 结果(缩短并削减前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

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() ? 我是否错过了Collections.sort()

Collections.sort uses stable sorting algorithm (modified merge sort), that is, if some items have equal keys on which the collection is being sorted, their mutual order will be intact after sorting. Collections.sort使用稳定的排序算法(修改后的合并排序),也就是说,如果某些项目具有相同的键,则在对它们进行排序时,它们的相互顺序在排序后将保持不变。

That's why reversing your collection changes ordering after sort. 这就是为什么撤消集合会更改排序后的顺序的原因。 Of course, only items with equal sorting keys will have different order. 当然,只有具有相同排序键的项目才具有不同的顺序。

Long.compare(long, long) should be used to compare long values. Long.compare(long, long)应该用于比较long值。

What's wrong with using == to compare floats in Java? 使用==比较Java中的浮点数有什么问题?


Consider the following collection of items, AF, each with an integer property, size . 考虑以下项目AF的集合,每个项目都有一个整数属性size

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

Collections.reverse() would do: Collections.reverse()将执行以下操作:

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

then Collections.sort() would reorder items based on the size, but otherwise leave the ordering intact: 然后Collections.sort()会根据大小对商品重新排序,但其他顺序保持不变:

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

such that if you just did Collections.sort() without reversing the collection first, A&B and C&F would stay in the same order: 这样,如果您只执行 Collections.sort()而没有先反转集合,A&B和C&F将保持相同的顺序:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM