我以map<integer,findeduser>格式对数据进行排序,其中integer是system.currenttimemillis()/1000和findeduser类中的时间戳
Map<Integer, FindedUser> reverseSortedMap = new TreeMap<>(Collections.reverseOrder());
reverseSortedMap.putAll(addedUsersMap);
for (Map.Entry<Integer, FindedUser> entry : reverseSortedMap.entrySet()) {
FindedUser user = entry.getValue();
System.out.println(entry.getKey()+" "+entry.getValue());
helperMap.put(entry.getKey(), new FindedUser(user.getId(), user.getName(), user.getTimestamp()));
}
得到了正确的数据
1609093171 FindedUser{name='ccc', id='11', timestamp='1609093171'}
1609092871 FindedUser{name='bbb', id='10', timestamp='1609092871'}
1609092015 FindedUser{name='aaa', id='9', timestamp='1609092015'}
但当我将其发送到listview时:
MyAdapterHash addedUsersAdapter = new MyAdapterHash(helperMap);
addedUsersListView.setAdapter(addedUsersAdapter);
addedUsersAdapter.notifyDataSetChanged();
结果排序不正确:
1609092015 FindedUser{name='aaa', id='9', timestamp='1609092015'}
1609093171 FindedUser{name='ccc', id='11', timestamp='1609093171'}
1609092871 FindedUser{name='bbb', id='10', timestamp='1609092871'}
问题出在哪里?
1条答案
按热度按时间dxpyg8gm1#
看起来像
helperMap
属于类型HashMap
它不保持插入顺序。更改类型
helperMap
至LinkedHashMap
这给了你一个可预测的迭代顺序。