如何将数组与另一个数组进行排序

时间:2022-09-15 07:46:32

What will the Java program be to perform the below task?

Java程序将执行以下任务是什么?

Given two arrays called a and c. I need to sort a with respect to c.

给定两个名为a和c的数组。我需要对c进行排序。

For example, if a={2,3,4} and c={-1,2,1}. Sorting a with regards to c will produce {2,4,3}

例如,如果a = {2,3,4}且c = { - 1,2,1}。对c进行排序将产生{2,4,3}

I did it in C++ using pair<>. How do I do the same in Java using inbuilt features?

我使用pair <>在C ++中完成了它。如何使用内置功能在Java中执行相同的操作?

3 个解决方案

#1


0  

You can do the same in Java:

您可以在Java中执行相同的操作:

class Pair<A, C extends Comparable<C>> implements Comparable<Pair<A,C>> {
    public final A a;
    public final C c;

    Pair(A a, C c) {
        this.a = a;
        this.c = c;
    }

    @Override
    public int compareTo(Pair<A, C> o) {
        return c.compareTo(o.c);
    }
}

...

public static void main(String[] args) {
    List<Pair<Integer,Integer>> list = new ArrayList<>();
    list.add(new Pair<>(2,-1));
    list.add(new Pair<>(3,2));
    list.add(new Pair<>(4,1));
    Collections.sort(list);
    list.stream().forEach((pair) -> {
        System.out.println(pair.a + " " + pair.c);
    });
}

UPDATE:

Or, more simply:

或者,更简单地说:

class Pair<A, C> {
    public final A a;
    public final C c;

    Pair(A a, C c) {
        this.a = a;
        this.c = c;
    }
}

public static void main(String[] args) {
    List<Pair<Integer,Integer>> list = new ArrayList<>();
    list.add(new Pair<>(2,-1));
    list.add(new Pair<>(3,2));
    list.add(new Pair<>(4,1));
    Collections.sort(list,
            (Pair<Integer, Integer> o1, Pair<Integer, Integer> o2) -> o1.c.compareTo(o2.c));
    list.stream().forEach((pair) -> {
        System.out.println(pair.a + " " + pair.c);
    });
}

#2


1  

Here is a possibility using arrays directly instead of intermediate data structures. It's a little bit more complicated (a couple of lines) than would ideally be necessary, but IntStream does not provide a sorted(Comparator), so the stream needs to be boxed to sort by the auxiliary array, and then unboxed.

这是一种直接使用数组而不是中间数据结构的可能性。它比理想情况下要复杂得多(几行),但IntStream不提供排序(Comparator),因此需要将流装箱以按辅助阵列排序,然后取消装箱。

int[] a = { 2, 3, 4 };
int[] c = { -1, 2, 1 };

int[] sorted = IntStream.range(0, a.length)
    .boxed()
    .sorted((n1, n2) -> Integer.compare(c[n1], c[n2]))
    .mapToInt(Integer::intValue)
    .map(i -> a[i])
    .toArray();

The algorithm computes a sorted permutation of c, and then outputs that permutation of a.

该算法计算c的排序排列,然后输出a的排列。

#3


0  

This algorithm should work:

该算法应该工作:

public static void main(String[] args) {
    List<Integer> a = Arrays.asList(2, 3, 4);
    List<Integer> c= Arrays.asList(-1, 2, 1);

    List<Integer> sa = new ArrayList<>(a);
    Collections.sort(sa);

    List<Integer> sc = new ArrayList<>(c);
    Collections.sort(sc);

    List<Integer> b = new ArrayList<>(a);
    for (int idx = 0; idx < sc.size(); idx++) {
        b.set(c.indexOf(sc.get(idx)), sa.get(idx));
    }
}

The List<Integer> b will contain the elements of a according the sort order in c.

List b将包含符合c中排序顺序的元素。

How it work:

它是如何工作的:

  • we sort the elements of a.
  • 我们对a的元素进行排序。

  • we sort the elements of c to identify their absolute position.
  • 我们对c的元素进行排序以确定它们的绝对位置。

  • we iterate over each absolute position of sorted c and identify the relative position of the element.
  • 我们遍历排序的c的每个绝对位置并识别元素的相对位置。

  • we lookup the element in a which corresponds to the same absolute position as the element in c and place it to the same relative position as the relative position of the element of c.
  • 我们查找a中的元素,该元素对应于与c中元素相同的绝对位置,并将其放置到与c元素的相对位置相同的相对位置。

#1


0  

You can do the same in Java:

您可以在Java中执行相同的操作:

class Pair<A, C extends Comparable<C>> implements Comparable<Pair<A,C>> {
    public final A a;
    public final C c;

    Pair(A a, C c) {
        this.a = a;
        this.c = c;
    }

    @Override
    public int compareTo(Pair<A, C> o) {
        return c.compareTo(o.c);
    }
}

...

public static void main(String[] args) {
    List<Pair<Integer,Integer>> list = new ArrayList<>();
    list.add(new Pair<>(2,-1));
    list.add(new Pair<>(3,2));
    list.add(new Pair<>(4,1));
    Collections.sort(list);
    list.stream().forEach((pair) -> {
        System.out.println(pair.a + " " + pair.c);
    });
}

UPDATE:

Or, more simply:

或者,更简单地说:

class Pair<A, C> {
    public final A a;
    public final C c;

    Pair(A a, C c) {
        this.a = a;
        this.c = c;
    }
}

public static void main(String[] args) {
    List<Pair<Integer,Integer>> list = new ArrayList<>();
    list.add(new Pair<>(2,-1));
    list.add(new Pair<>(3,2));
    list.add(new Pair<>(4,1));
    Collections.sort(list,
            (Pair<Integer, Integer> o1, Pair<Integer, Integer> o2) -> o1.c.compareTo(o2.c));
    list.stream().forEach((pair) -> {
        System.out.println(pair.a + " " + pair.c);
    });
}

#2


1  

Here is a possibility using arrays directly instead of intermediate data structures. It's a little bit more complicated (a couple of lines) than would ideally be necessary, but IntStream does not provide a sorted(Comparator), so the stream needs to be boxed to sort by the auxiliary array, and then unboxed.

这是一种直接使用数组而不是中间数据结构的可能性。它比理想情况下要复杂得多(几行),但IntStream不提供排序(Comparator),因此需要将流装箱以按辅助阵列排序,然后取消装箱。

int[] a = { 2, 3, 4 };
int[] c = { -1, 2, 1 };

int[] sorted = IntStream.range(0, a.length)
    .boxed()
    .sorted((n1, n2) -> Integer.compare(c[n1], c[n2]))
    .mapToInt(Integer::intValue)
    .map(i -> a[i])
    .toArray();

The algorithm computes a sorted permutation of c, and then outputs that permutation of a.

该算法计算c的排序排列,然后输出a的排列。

#3


0  

This algorithm should work:

该算法应该工作:

public static void main(String[] args) {
    List<Integer> a = Arrays.asList(2, 3, 4);
    List<Integer> c= Arrays.asList(-1, 2, 1);

    List<Integer> sa = new ArrayList<>(a);
    Collections.sort(sa);

    List<Integer> sc = new ArrayList<>(c);
    Collections.sort(sc);

    List<Integer> b = new ArrayList<>(a);
    for (int idx = 0; idx < sc.size(); idx++) {
        b.set(c.indexOf(sc.get(idx)), sa.get(idx));
    }
}

The List<Integer> b will contain the elements of a according the sort order in c.

List b将包含符合c中排序顺序的元素。

How it work:

它是如何工作的:

  • we sort the elements of a.
  • 我们对a的元素进行排序。

  • we sort the elements of c to identify their absolute position.
  • 我们对c的元素进行排序以确定它们的绝对位置。

  • we iterate over each absolute position of sorted c and identify the relative position of the element.
  • 我们遍历排序的c的每个绝对位置并识别元素的相对位置。

  • we lookup the element in a which corresponds to the same absolute position as the element in c and place it to the same relative position as the relative position of the element of c.
  • 我们查找a中的元素,该元素对应于与c中元素相同的绝对位置,并将其放置到与c元素的相对位置相同的相对位置。