所以我有 3 个数组,其中 1 个是字符串数组,2 个是 int 数组。
现在,当我对其中一个进行排序时,我必须对所有这些进行排序。
例如:
String[] stringarr = {"a", "z", "b"};
int[] intarra = {5, 8, 2};
int[] intarrb = {0, 7, 1};
我想要的是按 intarra 对它们进行排序。
我可以使用
Arrays.sort(intarra);
返回结果
intarra == {2, 5, 8};
但是我该如何对其他数组进行排序,以便
stringarr == {"b", "a", "z"};
intarrb == {1, 0, 7};
或者我应该使用其他东西来存储数据?
字符串数组是事件标题,而整数数组是事件发生的时间。
最佳答案
使用 OOP 可以轻松完成此操作。创建一个包含数据的结构并指定排序方法:
public class MyStructure implements Comparable
{
public String string;
public int int1;
public int int2;
public int compareTo(Object obj)
{
if (obj instanceof MyStructure)
{
return ((MyStructure) obj.int1) - int1;
}
return 0;
}
}
现在创建这些结构的数组并对该数组进行排序。
关于java - 对多个数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9545787/