Cael
Cael

Reputation: 556

Getting the index position of the sorted array from the original arrray

I have an array(distCent) that contains several double value.

double[] distCent = {0.34, 0.12, 0.01, 0.45, 0.65, 0.78};

I wanted to get the index position(x) of the top 5 lowest value in the array. My desired output would be something like this:

Smallest value is at position 3 with the value of 0.01
2nd smallest value is at position 2 with the value of 0.12
3rd smallest value is at position x with the value of y
4th smallest value is at position x with the value of y
5th smallest value is at position x with the value of y

To achieve that, I've sorted the array into lowest to highest as below:

Arrays.sort(distCent);//use sort 
  System.out.println(Arrays.asList(distCent)); //the value in the array will be sorted

Now, I am not sure how I could get the top 5 index position so that it will produce the output I expected or any other better way to achieve that? Anyone can help? Thanks!

Upvotes: 2

Views: 605

Answers (3)

Timothy
Timothy

Reputation: 26

import java.util.ArrayList;
...
ArrayList<double> temp = new ArrayList<double>();
for(double i : distCent){
    temp.add(i);
}
Arrays.sort(distCent);
for(int x = 0; x < 5; x++){
    //index is the original location
    int index = temp.indexOf(distCent[x]);
}

To keep the original indices, you'll need to create a copy of the original array, then check the values against the sorted array. Lists in Java have a handy indexOf method that does this.

Upvotes: 0

user4910279
user4910279

Reputation:

Try this.

double[] distCent = {0.34, 0.12, 0.01, 0.45, 0.65, 0.78};
String[] names = {"Smallest", "2nd smallest", "3rd smallest", "4th smallest", "5th smallest"};
int[] c = {0};
IntStream.range(0, distCent.length)
    .mapToObj(n -> new double[]{n, distCent[n]})
    .sorted(Comparator.comparing(a -> a[1]))
    .limit(names.length)
    .forEach(a -> System.out.printf("%s value is at position %d with the value of %.2f%n",
        names[c[0]++], (int)a[0] + 1, a[1]));

output

Smallest value is at position 3 with the value of 0.01
2nd smallest value is at position 2 with the value of 0.12
3rd smallest value is at position 1 with the value of 0.34
4th smallest value is at position 4 with the value of 0.45
5th smallest value is at position 5 with the value of 0.65

Upvotes: 2

Use Objects to pair up the data values with an index.

Define a Pair class, for example:

public class Pair implements Comparable<Pair> {
    double value;
    int index;

    public Pair(double v, int i) {
        value = v;
        index = i;
    }
    public int compareTo(Pair p) {
        if (value - p.value < 0) return -1;
        if (value - p.value > 0) return 1;
        return 0;
}

Then create your array.

Pair[] distCent = {new Pair(0.34, 0), new Pair(....)};

Now, after sorting, when you access the array, you can see the index.

distCent[i].index //this is the original index of the item

I recommend using a custom print method instead of the asList method, as it offers more flexibility. You can print the indices with your custom method.

Upvotes: 2

Related Questions