根据特定顺序对双数组进行排序

时间:2016-09-06 06:20:55

标签: java arrays sorting comparator

我正在尝试将数组排序为特定顺序,例如:我想根据顺序或map将数组[2, 0, 1, 2]排序为arr。但我收到了错误。

double[] arr = {3, 1.3, 2, 3};
int[] map = {0, 1, 2, 3};
// I want to get [2, 0, 1, 2]
Collections.sort(map, (a,b) -> a.compareTo(b));

这只是一个伪代码,我知道它不起作用。如何在不使用双循环的情况下实现这一目标?

编辑 结果数组[2,0,1,2]应该计算为arr中数字的等级。 1.3是最小的,所以它得到等级0. 2得到等级1,并且3得到等级2.

3 个答案:

答案 0 :(得分:2)

这很有效(虽然它是一个非常不优雅的解决方案),但它并没有使用双循环,但我猜你正在寻找一个优雅的解决方案。

public class IntegerOrderMapper {

    public int[] map(int[] input) {
        int[] output = new int[input.length];
        int[] sorted = input.clone(); // 3, 1.3, 2, 3
        Arrays.sort(sorted); // 1.3, 2, 3, 3

        Set<Integer> set = new HashSet<Integer>();
        for(int i = 0; i < sorted.length; i++) { // removes duplicates
            set.add(sorted[i]); // 1.3, 2, 3
        }
        for(int i = 0; i < input.length; i++) {
            output[i] = Arrays.asList(set.toArray()).indexOf(input[i]);
        }
        return output;
    }
}

测试类:

public class IntegerTest {

    public static void main(String[] args) {
        IntegerOrderMapper iom = new IntegerOrderMapper();
        int[] result = iom.map(new int[] {3, 1, 2, 3}); // <- using integers here

        for(int i : result) {
            System.out.println(i); // <- prints 2, 0, 1, 2
        }
    }
}

此版本仅适用于整数数组,但您可以创建一个通用版本来处理任何对象。

答案 1 :(得分:2)

如果您正在寻找findRank()代码的效率,请不要在循环中多次调用list.indexOf()。在列表中查找元素的时间复杂度是O(n)。对于O(1)复杂度,请使用Map而不是List。我假设您可以轻松地将List替换为Map实施。

答案 2 :(得分:1)

我使用以下代码

对java8中的数组进行了排序
double[] da = {99, 11, 7, 21, 4, 2};
da = Arrays.stream(da)
    .boxed()
    .sorted((a, b) -> a.compareTo(b)) //using comparator
    .mapToDouble(i -> i)
    .toArray();

我实际上想在排序数组后找到每个项目的等级。所以我发现了以下功能。

private static int[] findRank(double[] x){
    //declare empty list and rank array
    List<Double> lst = new ArrayList<Double>();
    int[] rank=new int[x.length]; // maximum length for already unique array
    //add only unique elements of x in the list
    for(double d:x)
        if (lst.indexOf(d) == -1)
            lst.add(d);

    Collections.sort(lst);
    for(int i=0;i<x.length;i++) {
        rank[i]=lst.indexOf(x[i]);
    }
    return rank;
}

我感兴趣的是更好地实现上述代码。特别是循环部分。