简体   繁体   English

对两个ArrayList进行排序,一个相对于另一个

[英]Sort two ArrayLists, one relative to the other

I want to sort one ArrayList used to display to the user: 我想对用于显示给用户的一个ArrayList进行排序:

String[] listOne = {whip, amulet, cape};

Then sort a second ArrayList used as backed data relative to the first list being sort: 然后,相对于要排序的第一个列表,对用作支持数据的第二个ArrayList进行排序:

String[] listTwo = {abyssal_whip, amulet_of_power, strength_cape};

Is there any way I can do this? 有什么办法可以做到吗?

Don't store two related pieces of data in separate arrays. 不要将两个相关的数据存储在单独的数组中。

Instead create a custom Object that contains both pieces of data. 而是创建一个包含两个数据段的自定义对象。 Then you can implement Comparable on your class or write a custom Comparator to do the sorting for you. 然后,您可以在您的类上实现Comparable或编写自定义Comparator来为您进行排序。

For example check out: Sorting using Comparator- Descending order (User defined classes) 例如,检出: 使用比较器降序排序(用户定义的类)

for an example that shows you how to: 为示例向您展示如何:

  1. create a custom Object 创建一个自定义对象
  2. create a custom Comparator or implement Comparable . 创建自定义Comparator或实现Comparable
  3. use the Collections class to do the sorting. 使用Collections类进行排序。

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

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