简体   繁体   English

如何对两个 arrays 进行排序,其中一个是基于另一个排序的?

[英]How to sort two arrays with one being sorted based on the sorting of the other?

So I have encountered this problem a lot of times.所以我经常遇到这个问题。 Let me explain.让我解释。 Say I have these two arrays: A1={1,2,3,4,5,6,7,8,9,10};假设我有这两个 arrays: A1={1,2,3,4,5,6,7,8,9,10}; and A2={1,2,3,0,2,1,1,0,0,0}; A2={1,2,3,0,2,1,1,0,0,0}; . . What I require is this: When I sort A2, whatever swapping and shifting of elements takes place in A2, the same should take place in A1 as well.我需要的是:当我对 A2 进行排序时,无论在 A2 中发生什么交换和移动元素,在 A1 中也应该发生同样的事情。 Basically I am trying to create a Map using two arrays instead of creating an actual HashMap or HashTable.基本上,我正在尝试使用两个 arrays 创建一个 Map,而不是创建一个实际的 HashMap 或 HashTable。

Finally the arrays should look like this: A1={4,8,9,10,1,6,7,2,5,3};最后 arrays 应该如下所示: A1={4,8,9,10,1,6,7,2,5,3}; and A2={0,0,0,0,1,1,1,2,2,3}; A2={0,0,0,0,1,1,1,2,2,3}; . . The corresponding values of both arrays are still the same but the data is sorted based on A2. arrays的对应值还是一样的,但是数据是按照A2排序的。 I need a way to do this kind of sort in the fastest way possible.我需要一种方法以尽可能快的方式进行这种排序。

Why am I doing this?我为什么要这样做? Well, say in a question, there's an array of numbers given and I am required to print all the numbers of the array in order of increasing occurrences.好吧,在一个问题中,给出了一个数字数组,我需要按照出现次数增加的顺序打印数组的所有数字。 Say A1 denotes the numbers in given array and A2 denotes the corresponding number's occurrences.假设 A1 表示给定数组中的数字,A2 表示相应数字的出现次数。 Using arrays, I can easily sort both arrays in the above manner and iterate though A1 using indices and print the numbers.使用 arrays,我可以轻松地以上述方式对 arrays 进行排序,并使用索引遍历 A1 并打印数字。 One might suggest a HashMap and to sort it but it accesses values though keys so the sorting wont make a lot of sense I think.有人可能会建议 HashMap 并对其进行排序,但它通过键访问值,因此我认为排序不会有很大意义。 I'm sure there's a better way to achieve what I'm trying to do.我确信有更好的方法来实现我想要做的事情。 Please suggest either a good way to sort or any better approach for the problem.请提出一个好的排序方法或任何更好的方法来解决这个问题。 Thanks in advance!提前致谢!

Pair Class could do the trick here.配对 Class 可以在这里解决问题。

import java.util.*;
public class Main
{
    static class Pair implements Comparable<Pair>
    {
        int a1;
        int a2;
        Pair (int a1, int a2) //constructor 
        {
            this.a1 = a1;
            this.a2 = a2;
        }
        public int compareTo(Pair other) //making it only compare a2 values
        {
            return this.a2 - other.a2;
        }
    }
    public static void main(String[] args) 
    {
        int[] A1 = {1,2,3,4,5,6,7,8,9,10};
        int[] A2 = {1,2,3,0,2,1,1,0,0,0};
        Pair[] pairs = new Pair[A1.length];
        for (int i = 0; i < pairs.length; i++)
        {
            pairs[i] = new Pair(A1[i], A2[i]);
        }
        Arrays.sort(pairs);
        //printing values 
        for (int i = 0; i < A1.length; i++)
        {
            System.out.print(pairs[i].a1 + " ");
        }
        System.out.println();
        for (int i = 0; i < A2.length; i++)
        {
            System.out.print(pairs[i].a2 + " ");
        }
    }
}

By making a Pair class that holds 2 variables a1 and a2 , you can override the compareTo method to only compare the a2 value, so that when Arrays.sort is called, the pairs in the Pair array will be swapped only according to the a2 values.通过制作包含 2 个变量a1a2的 Pair class ,您可以重写compareTo方法以仅比较a2值,这样当Arrays.sort被调用时,Pair 数组中的对将仅根据a2值交换. You can then access the values in the pairs and print them out.然后,您可以访问对中的值并将它们打印出来。 This will produce your desired output.这将产生您想要的 output。

You can create a two-dimensional array where each element is an array of length 2 and sort it based on the second element.您可以创建一个二维数组,其中每个元素都是长度为2的数组,并根据第二个元素对其进行排序。

int[] A1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, A2 = { 1, 2, 3, 0, 2, 1, 1, 0, 0, 0 };
final int[][] res = new int[A1.length][2];
for(int i = 0; i < res.length; i++) {
    res[i] = new int[] {A1[i], A2[i]};
}
Arrays.sort(res, (a,b)->Integer.compare(a[1], b[1]));
//Alternatively, Arrays.sort(res, Comparator.comparingInt(a -> a[1]));
for(final int[] a : res) {
    System.out.println(a[0] + " " + a[1]);
}

You could try creating an object based on the data that you actually have.您可以尝试根据您实际拥有的数据创建 object。 In this case the object might contain two fields, numbers and occurrences.在这种情况下,object 可能包含两个字段,数字和出现次数。 Then implement a comparator to compare by the occurrences field.然后实现一个比较器以按出现字段进行比较。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Java中基于比率对两个数组排序? - How to sort two arrays based on ratio in Java? 如何将 Java 中的两个 arrays 合并为一个排序的 ArrayList? - How to merge two arrays in Java into one sorted ArrayList? 如何对彼此相关的两个数组进行排序? - How do I sort two arrays in relation to each other? 如何相对于彼此对两个 arrays 进行排序? - How do I sort two arrays relative to each other? 如何像其他排序列表一样对 ArrayList 进行排序? - How to sort ArrayList like other sorted list? Arrays.sort(…)对已排序的200万个元素数组的效率如何 - How efficient is Arrays.sort(…) for a 2 Million elements arrays that is already sorted 根据基于两个数组的值对数组进行排序 - Sorting an array by its values based on two arrays 如何根据一个数组的值对两个数组排序 - How to sort two arrays according to one array's values 在构面上进行排序后,如何维护行映射? 如果我在1个方面进行排序,则其他列值不会得到相应的排序 - How can I maintain the row mapping after sorting on facets? If I sort on 1 facet, other column values don't get sorted accordingly 按降序对两个数组进行排序,一个是double arraylist,另一个是string arraylist - Sorting two arrays in descending order, one is double arraylist, the other is string arraylist
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM