Java 数组排序

问题描述 投票:0回答:1

我可以根据第二个数组对数组进行排序吗?

我的意思是,让我们说

a[]=[2,5,4,3]
c[]=[800,1600,200,400]

c[i]
a[i]
与相同的
i
相关。

我想对

a[]=[5,2,3,4]
按存储在
c
中的值的降序进行排序,因为
5->1600
2->800
等等。

可以在一行中完成吗,如下所示(下面只是我期望的答案结构)

Arrays.sort(a, (i, j) -> Integer.compare(c[j], c[i]))

我正在通过使用额外空间的冗长方法来做到这一点。

java arrays sorting
1个回答
0
投票

我创建了一个名为

sortParallelArrays
的函数,它接受两个整数(在您的情况下),或者一个可比较的数组和一个关联数组。

以下是所涉及步骤的细分:

  1. 第一步是将可比较数组转换为索引
  2. 接下来,对索引进行排序
  3. 最后,按排序后的索引对两个源数组进行排序
public static void sortParallelArrays(int[] comparable, int[] associated, boolean reverse) {
    Integer[] indices = toIndices(comparable);
    Comparator<Integer> comparator = Comparator.comparing((Integer i) -> comparable[i]);
    Arrays.sort(indices, reverse ? comparator.reversed() : comparator);
    reorderInPlace(comparable, indices);
    reorderInPlace(associated, indices);
}

private static Integer[] toIndices(int[] arr) {
    Integer[] indices = new Integer[arr.length];
    for (int i = 0; i < arr.length; i++) indices[i] = i;
    return indices;
}

private static void reorderInPlace(int[] comparable, Integer[] indices) {
    int[] temp = Arrays.copyOf(comparable, comparable.length);
    for (int i = 0; i < indices.length; i++) comparable[i] = temp[indices[i]];
}

带有泛型的完整演示

这是一个涉及两个原始整数数组和另一个具有不同对象数组的场景。

package org.example;

import java.util.Arrays;
import java.util.Comparator;

public class ParallelSort {
    public static void main(String[] args) {
        // Sort two parallel integer primitive arrays
        int[] a = { 2, 5, 4, 3 }, c = { 800, 1600, 200, 400 };
        sortParallelArrays(a, c, true);
        System.out.println("Sorted a: " + Arrays.toString(a));
        System.out.println("Sorted c: " + Arrays.toString(c));

        // Sort an array of strings with associate characters
        String[] b = { "c", "b", "a", "d" };
        Character[] d = { 'w', 'x', 'z', 'y' };
        sortParallelArrays(b, d);
        System.out.println("Sorted b: " + Arrays.toString(b));
        System.out.println("Sorted d: " + Arrays.toString(d));
    }

    public static <C extends Comparable<C>, A> void sortParallelArrays(C[] comparable, A[] associated, boolean reverse) {
        Integer[] indices = toIndices(comparable);
        Comparator<Integer> comparator = Comparator.comparing((Integer i) -> comparable[i]);
        Arrays.sort(indices, reverse ? comparator.reversed() : comparator);
        reorderInPlace(comparable, indices);
        reorderInPlace(associated, indices);
    }

    public static <C extends Comparable<C>, A> void sortParallelArrays(C[] comparable, A[] associated) {
        sortParallelArrays(comparable, associated, false);
    }

    public static void sortParallelArrays(int[] comparable, int[] associated, boolean reverse) {
        Integer[] indices = toIndices(comparable);
        Comparator<Integer> comparator = Comparator.comparing((Integer i) -> comparable[i]);
        Arrays.sort(indices, reverse ? comparator.reversed() : comparator);
        reorderInPlace(comparable, indices);
        reorderInPlace(associated, indices);
    }

    public static void sortParallelArrays(int[] comparable, int[] associated) {
        sortParallelArrays(comparable, associated, false);
    }

    private static <E extends Comparable<E>> Integer[] toIndices(E[] arr) {
        Integer[] indices = new Integer[arr.length];
        for (int i = 0; i < arr.length; i++) indices[i] = i;
        return indices;
    }

    private static Integer[] toIndices(int[] arr) {
        Integer[] indices = new Integer[arr.length];
        for (int i = 0; i < arr.length; i++) indices[i] = i;
        return indices;
    }

    private static <E> void reorderInPlace(E[] comparable, Integer[] indices) {
        E[] temp = Arrays.copyOf(comparable, comparable.length);
        for (int i = 0; i < indices.length; i++) comparable[i] = temp[indices[i]];
    }

    private static void reorderInPlace(int[] comparable, Integer[] indices) {
        int[] temp = Arrays.copyOf(comparable, comparable.length);
        for (int i = 0; i < indices.length; i++) comparable[i] = temp[indices[i]];
    }
}

输出

Sorted a: [5, 4, 3, 2]
Sorted c: [1600, 200, 400, 800]
Sorted b: [a, b, c, d]
Sorted d: [z, x, w, y]
© www.soinside.com 2019 - 2024. All rights reserved.