You can create an object holding both relevance and index at the same time and put that object in a new List. Now you can sort that list by relevance and get the corresponding indexes.
Something like that:
// Class holding relevance and index at the same time
public class RelevanceIndex {
private int index;
private double relevance;
...
}
// Create and populate a list of RelevanceIndex
List<RelevanceIndex> relevanceIndexes = new ArrayList<>();
for (int i = 0; i < indices.length; i++) {
RelevanceIndex relevanceIndex = new RelevanceIndex();
relevanceIndex.setIndex(indexes[i]);
relevanceIndex.setRelevance(relevances[i]);
relevanceIndexes.add(relevanceIndex);
}
...
// Sort relevanceIndexes by relevance using method sort of List
// (you need to define your Comparator or define RelevanceIndex as
// Comparable)
// Now you have the related indexes sorted. If necessary you can put them
// in a new sorted array
EDIT: Added a complete implementation of this answer
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class ArraySorting {
public static void main(String[] args) {
int[] indices = {5, 8, 3, 2};
double[] relevance = {0.1234, 0.3567, 0.2254, 0.0005};
ArraySorting app = new ArraySorting();
app.run(indices, relevance);
}
void run(int[] indices, double[] relevance) {
List<RelevanceIndex> relevanceIndices = getRelevanceIndices(indices, relevance);
System.out.println(relevanceIndices);
Collections.sort(relevanceIndices);
System.out.println(relevanceIndices);
}
List<RelevanceIndex> getRelevanceIndices(int[] indices, double[] relevance) {
List<RelevanceIndex> relevanceIndices = new ArrayList<>();
for (int i = 0; i < indices.length; i++) {
relevanceIndices.add(new RelevanceIndex(indices[i], relevance[i]));
}
return relevanceIndices;
}
class RelevanceIndex implements Comparable<RelevanceIndex> {
private int index;
private double relevance;
RelevanceIndex(int index, double relevance) {
this.index = index;
this.relevance = relevance;
}
@Override
public int compareTo(RelevanceIndex other) {
return Double.compare(this.relevance, other.relevance);
}
@Override
public String toString() {
return String.format("%s (%s)", index, relevance);
}
}
}
Map<Double, Integer> accumulator = new TreeMap<>(Comparator.naturalOrder()); accumulator.put(0.1234, 5); accumulator.put(0.3567, 8); accumulator.put(0.2254, 3); accumulator.put(0.0005, 2); System.out.println(accumulator);