我有一个ArrayList和一个String数组。String数组包含IDs,而ArrayList包含与这些IDs相关的信息。但是,这个ArrayList的顺序不尽如人意。我有一个String数组,其中按照我想要它们在ArrayList中的顺序存储了这些IDs。
半伪代码示例:
我该如何根据字符串数组中的Id重新排序ArrayList,从而得到以下结果:
半伪代码示例:
ArrayList<MyObject> myList = new ArrayList<MyObject>();
for (every username)
{
myList.add(new MyObject(id, username, content, country);
}
String[] ids = new String[myList.size()];
...Ids are added and sorted here...
我现在有一个Id列表,按照正确的顺序排列。 "myList" 中的每个 Id 对应于 "ids" 字符串数组中的一个 Id。我想根据 "ids" 字符串数组中对应的 id 的顺序对 "myList" 进行排序。
我该如何以这种方式重新对我的 ArrayList 进行排序呢?
Eg. if in Array list I have:
1. 123, Bob, test, USA
2. 1234, Vladimir, test, USA
3. 12345, Yoseph, test, USA
and in the String[] I have:
1. 1234
2. 123
3.12345
我该如何根据字符串数组中的Id重新排序ArrayList,从而得到以下结果:
1. 1234, Vladimir, test, USA
2. 123, Bob, test, USA
3. 12345, Yoseph, test, USA