【问题标题】:Java: Sort an Array Based on the Index Order of Another ArrayJava:根据另一个数组的索引顺序对数组进行排序
【发布时间】:2020-07-27 19:24:26
【问题描述】:

在 Java 中,如何根据另一个已排序数组的索引顺序对数组进行排序?例如,如果我有:

arr1 = {26, 8, 3}
arr2 = {3, 1, 2}
arr3 = {57, 23, 11}
arr4 = {78, 2, 61}

我将 arr2 按升序排序为

arr2 = {1, 2, 3}

然后我希望另一个是:

arr1 = {8, 3, 26}
arr3 = {23, 11, 57}
arr4 = {2, 61, 78}

我怎样才能做到这一点是Java?我知道我会将新的排序数组保存到新的实例中。有什么帮助,谢谢!

【问题讨论】:

  • 如果你有一个对象数组而不是四个模糊相关的整数数组,这会容易得多。
  • 不幸的是,我需要将数组分开以便使用不同的方法。不过你是对的。
  • 为什么需要将它们分开以将它们用于不同的方法?此外,是什么阻止您将它们捆绑到对象中、执行排序然后再将它们解包?
  • 存储arr2的原始位置,例如。 {1, 2, 0 } 来跟踪 1 来自索引 1、2 来自索引 2 和 3 来自索引 0。一旦有了原始位置,就可以使用相同的顺序对其他数组进行排序。
  • 如何对arr2 进行排序?

标签: java arrays sorting indexing


【解决方案1】:

在别处找到答案

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();
    }
}

【讨论】:

    【解决方案2】:

    这是一种方法。

    • 根据数组内容对目标数组的索引进行排序。
    • 然后使用该索引数组根据索引数组映射所有数组。
    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());
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-11-10
      • 1970-01-01
      • 2019-08-04
      • 1970-01-01
      • 2013-12-24
      • 2021-08-12
      • 2015-06-08
      相关资源
      最近更新 更多