英文:
Java: Sort an Array Based on the Index Order of Another Array
问题
In Java, you can sort one array based on the index order of another sorted array by creating a custom comparator and using it with a sorting algorithm like Arrays.sort(). Here's an example of how you can do it:
import java.util.Arrays;
import java.util.Comparator;
public class Main {
public static void main(String[] args) {
int[] arr1 = {26, 8, 3};
int[] arr2 = {3, 1, 2};
int[] arr3 = {57, 23, 11};
int[] arr4 = {78, 2, 61};
// Sort arr2 in ascending order
Arrays.sort(arr2);
// Create an index array to track the original positions
Integer[] indexes = new Integer[arr2.length];
for (int i = 0; i < indexes.length; i++) {
indexes[i] = i;
}
// Sort arr1, arr3, and arr4 based on the sorted order of arr2
Arrays.sort(indexes, Comparator.comparingInt(i -> arr2[i]));
int[] newArr1 = new int[arr1.length];
int[] newArr3 = new int[arr3.length];
int[] newArr4 = new int[arr4.length];
for (int i = 0; i < arr2.length; i++) {
newArr1[i] = arr1[indexes[i]];
newArr3[i] = arr3[indexes[i]];
newArr4[i] = arr4[indexes[i]];
}
// newArr1, newArr3, and newArr4 now contain the sorted values
System.out.println("arr1 = " + Arrays.toString(newArr1));
System.out.println("arr3 = " + Arrays.toString(newArr3));
System.out.println("arr4 = " + Arrays.toString(newArr4));
}
}
This code first sorts arr2
, then creates an index array to track the original positions. After that, it sorts arr1
, arr3
, and arr4
based on the sorted order of arr2
using the index array.
英文:
In Java, how can I sort an array based on the index order of another sorted array? For instance, if I have:
arr1 = {26, 8, 3}
arr2 = {3, 1, 2}
arr3 = {57, 23, 11}
arr4 = {78, 2, 61}
and I sort arr2 in ascending order to be
arr2 = {1, 2, 3}
and I want the other to then be:
arr1 = {8, 3, 26}
arr3 = {23, 11, 57}
arr4 = {2, 61, 78}
How can I accomplish this is Java? I know I would save the new sorted arrays into new instances. Anything helps, thanks!
答案1
得分: 0
这是一种方法来完成它。
- 根据数组的内容对目标数组的索引进行排序。
- 然后使用该索引数组将所有数组映射到基于索引的数组上。
Integer[] indices = IntStream.range(0, arr2.length)
.boxed()
.sorted(Comparator.comparing(i -> arr2[i]))
.toArray(Integer[]::new);
List<int[]> list = Stream
.of(arr1, arr2, arr3, arr4).map(arr -> Stream
.of(indices)
.mapToInt(i -> arr[i])
.toArray())
.collect(Collectors.toList());
list.forEach(arr -> System.out.println(Arrays.toString(arr)));
打印结果如下:
[8, 3, 26]
[1, 2, 3]
[23, 11, 57]
[2, 61, 78]
您还可以将这些数组放入另一个“2D”数组中,然后按照相同的方式进行操作,结果相同。
int[][] arrays = { arr1, arr2, arr3, arr4 };
List<int[]> list = Arrays
.stream(arrays)
.map(arr -> Stream
.of(indices)
.mapToInt(i -> arr[i])
.toArray())
.collect(Collectors.toList());
注意:上述代码示例中的字符转义是HTML实体编码,您可能需要将其还原为标准的字符。
英文:
Here is one way to do it.
- sort the indices of the target array based on the arrays contents.
- then use that index array to map all the arrays based on the indexed one.
Integer[] indices = IntStream.range(0, arr2.length)
.boxed()
.sorted(Comparator.comparing(i -> arr2[i]))
.toArray(Integer[]::new);
List<int[]> list = Stream
.of(arr1, arr2, arr3, arr4).map(arr -> Stream
.of(indices)
.mapToInt(i -> arr[i])
.toArray())
.collect(Collectors.toList());
list.forEach(arr -> System.out.println(Arrays.toString(arr)));
Prints
[8, 3, 26]
[1, 2, 3]
[23, 11, 57]
[2, 61, 78]
You could also place the arrays in another "2D"
array and do as follow with the same result.
int[][] arrays = { arr1, arr2, arr3, arr4 };
List<int[]> list = Arrays
.stream(arrays)
.map(arr -> Stream
.of(indices)
.mapToInt(i -> arr[i])
.toArray())
.collect(Collectors.toList());
</details>
# 答案2
**得分**: 0
以下是代码的翻译部分:
```java
public class SortTogether {
// 对数组 a 进行排序,并根据 a 的索引更新数组 b、c 和 d 中的元素
public static void bubbleSort(int[] a, int[] b, int[] c, int[] d) {
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a.length - i - 1; j++) {
if (a[j] > a[j + 1]) {
// 在交换元素时
int t = a[j];
a[j] = a[j + 1];
a[j + 1] = t;
// 同时交换其他数组中的元素
// 以确保其他数组中的元素也保持在一起
t = b[j];
b[j] = b[j + 1];
b[j + 1] = t;
t = c[j];
c[j] = c[j + 1];
c[j + 1] = t;
t = d[j];
d[j] = d[j + 1];
d[j + 1] = t;
}
}
}
}
public static void main(String a[]) {
int[] arr1 = {26, 8, 3};
int[] arr2 = {3, 1, 2};
int[] arr3 = {57, 23, 11};
int[] arr4 = {78, 2, 61};
System.out.println("排序前");
display(arr1);
display(arr2);
display(arr3);
display(arr4);
bubbleSort(arr2, arr1, arr3, arr4);
System.out.println("\n排序后");
display(arr1);
display(arr2);
display(arr3);
display(arr4);
}
public static void display(int[] arr) {
for (int num : arr) System.out.printf("%4d", num);
System.out.println();
}
}
请注意,我已经将 HTML 实体 <
和 "
替换为了正常的符号。
英文:
Found answer elsewhere
public class SortTogether{
// sort the array a, and also update the elements in array b, c, and d
// based on the index of a
public static void bubbleSort(int[] a, int[] b, int[] c, int[] d) {
for(int i=0; i<a.length; i++){
for(int j=0; j<a.length-i-1;j++){
if(a[j]>a[j+1]){
// when you are swapping the elements
int t = a[j]; a[j]=a[j+1];a[j+1]=t;
// swap the elements in the other arrays as well
// so the elements in other array will also stay together
t = b[j]; b[j]=b[j+1];b[j+1]=t;
t = c[j]; c[j]=c[j+1];c[j+1]=t;
t = d[j]; d[j]=d[j+1];d[j+1]=t;
}
}
}
}
public static void main(String a[]) {
int[] arr1 = {26, 8, 3};
int[] arr2 = {3, 1, 2};
int[] arr3 = {57, 23, 11};
int[] arr4 = {78, 2, 61};
System.out.println("Before sort");
display(arr1);
display(arr2);
display(arr3);
display(arr4);
bubbleSort(arr2,arr1,arr3,arr4);
System.out.println("\nAfter sort");
display(arr1);
display(arr2);
display(arr3);
display(arr4);
}
public static void display(int[] arr) {
for (int num : arr) System.out.printf("%4d", num);
System.out.println();
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论