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)
Prints
[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());
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);
Prints
[1, 2, 9, 9, 9, 9]
[2, 4, 6, 5, 3, 1]
The FuelInfo class
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);
}
}