简体   繁体   English

查找原始的二维排序数组索引

[英]Finding original 2d sorted array indexes

I've got this array 我有这个数组

Array    Index
[3,5]  - 0
[1,7]  - 1
[12,4] - 2
[3,2]  - 3

And I'm using this sort 我正在使用这种

Arrays.sort(array, (Integer[] int1, Integer[] int2) -> {
        Integer numberOfKeys1 = int1[1];
        Integer numberOfKeys2 = int2[1];
        return numberOfKeys1.compareTo(numberOfKeys2);
});

To get 要得到

Array    Index    Index I want 
[3,2]  - 0      - 3
[12,4] - 1      - 2
[3,5]  - 2      - 0
[1,7]  - 3      - 1

So my question is. 所以我的问题是。 How can I get original indexes of sorted 2d array? 如何获得排序的二维数组的原始索引?

Added 3rd row with indexes 添加了带有索引的第三行

array[i][2] = i;

and then ran the sort. 然后进行排序。 Solved, working. 解决,工作。

You could define a class containing an array and the original index. 您可以定义一个包含数组和原始索引的类。

class ArrayIndex {

    Integer[] data;
    int originalIndex;
}

and then sort an ArrayIndex[] array like this : 然后像这样对ArrayIndex[] array进行排序:

Arrays.sort(array, (ArrayIndex a, ArrayIndex b) -> 
        Integer.compare(a.data[1], b.data[1])
);

Note above how the Integer.compare method simplifies the comparison and avoids boxing. 请注意上面的Integer.compare方法如何简化比较并避免装箱。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM