如果存在重复,如何相对于另一个数组对一个数组排序? [英] How to sort an array with respect to another array if there are duplicates?

查看:70
本文介绍了如果存在重复,如何相对于另一个数组对一个数组排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果有唯一值,我可以相对于另一个对数组进行排序.但是由于我正在尝试对下面的给定数组进行排序:

I could sort an array with respect to another if there is unique value. But since i am trying sort the below given array:

initial fuel[]={1,9,9,2,9,9};
initial cost[]={2,6,5,4,3,1};

我想对燃料阵列进行排序,然后又想相对于燃料阵列对成本阵列进行排序.
我想要如下所示的输出:

I wanted to sort fuel array then again wanted to sort cost array with respect to fuel array.
I wanted output like given below:

final fuel[]={1,2,9,9,9,9};
final cost[]={2,4,6,5,3,1};

下面是我的代码:

public static void sort(List<Integer> c, List<Integer> f) {
    List<Integer> cc = new ArrayList<>(c);
    Collections.sort(c);

    List<Integer> al = new ArrayList<>();

    int i = 0;
    while (i < c.size()) {
        int temp = c.get(i);
        int index = cc.indexOf(temp);
        al.add(f.get(index));

        cc.remove(index);
        f.remove(index);

        i++;
    }

    for (int value : c)
        System.out.print(value + " ");
    System.out.println();
    for (int value : al)
        System.out.print(value + " ");
}

如何使用比较器以这种方式进行排序?还要如何使用流api?

How can I use comparator to sort in this ways? Also how to use stream api for the same?

推荐答案

如果您始终希望将给定的燃料值与成本相关联,那么我建议您创建一个类来保存它们.这样可以在将两个值保持在一起的同时进行适当的排序.

If you always want to associate a given fuel value with cost then I suggest you create a class to hold them. This will allow proper sorting while keeping both values together.

int[] fuel = { 1, 9, 9, 2, 9, 9 };
int[] cost = { 2, 6, 5, 4, 3, 1 };

这将创建类并根据燃料值对其进行排序,并将其放入列表中.

This creates the class and sorts it based on the fuel value, and puts it in a list.

List<FuelInfo> fuelInfo =
        // generate array indices
        IntStream.range(0, fuel.length)
        
        // create the objects 
        .mapToObj(i -> new FuelInfo(fuel[i], cost[i]))

        // sort them based on fuel value 
        .sorted(Comparator.comparing(FuelInfo::getFuel))

        // put them in a list.
        .collect(Collectors.toList());

 
fuelInfo.forEach(System.out::println)

打印

[1, 2]
[2, 4]
[9, 6]
[9, 5]
[9, 3]
[9, 1]

您可以按照以下步骤将各个值复制回列表:

You can copy the individual values back to a list as followis:

List<Integer> fuelList = fuelInfo.stream()
        .map(FuelInfo::getFuel).collect(Collectors.toList());
List<Integer> costList = fuelInfo.stream()
        .map(FuelInfo::getCost).collect(Collectors.toList());

如果您想保留重复项的默认顺序,那么这将起作用,因为Java中的排序是稳定的(比较相等值时将保持插入顺序).通过根据燃料数组的值对索引进行排序,然后使用排序后的索引以正确排序的顺序构建成本列表,可以实现这一目的.

If you want to maintain the default order for duplicates then this will work since the sorting in Java is stable (insertion order is maintained when comparing equals values). This works by using sorting the indices based on the value of the fuel array and then using the sorted indices to build the cost list in properly sorted order.

Comparator<Integer> comp = (a,b)->Integer.compare(fuel[a],fuel[b]);

Comparator<Integer> comp =
        (a, b) -> Integer.compare(fuel[a], fuel[b]);

List<Integer> sortedFuel = Arrays.stream(fuel).sorted()
        .boxed().collect(Collectors.toList());

List<Integer> sortedCost = IntStream.range(0, fuel.length)
        .boxed().sorted(comp).map(a -> cost[a])
        .collect(Collectors.toList());

System.out.println(sortedFuel);
System.out.println(sortedCost);

打印

[1, 2, 9, 9, 9, 9]
[2, 4, 6, 5, 3, 1]

FuelInfo类


class FuelInfo{
    private int fuelAmt;
    private int cost;
    public FuelInfo(int fuel, int cost) {
        this.fuelAmt = fuel;
        this.cost = cost;
    }
    public int getFuel() {
        return fuelAmt;
    }
    public int getCost() {
        return cost;
    }
    public String toString() {
        return String.format("[%s, %s]", fuelAmt, cost);
    }
}

这篇关于如果存在重复,如何相对于另一个数组对一个数组排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆