Source indexes of sorted ArrayList elements

The following code implements nfit sorting in ascending order.

 public static void main(String[] args) { ArrayList<Double> nfit = new ArrayList<Double>(); nfit.add(2.0); nfit.add(5.0); nfit.add(1.0); nfit.add(8.0); nfit.add(3.0); // Sort individuals in ascending order Collections.sort(nfit); System.out.print(nfit); } 

Output:

 [1.0, 2.0, 3.0, 5.0, 8.0] 

My question is how to get the starting indices of the sorted elements? In this example, the answer to my question will be as follows:

 [2, 0, 4, 1, 3] 

How can I get these indexes?

+4
source share
6 answers

Copy the ArrayList and do the sort, then use indexOf.

 ArrayList<Double> nfit = new ArrayList<Double>(); nfit.add(2.0); nfit.add(5.0); nfit.add(1.0); nfit.add(8.0); nfit.add(3.0); ArrayList<Double> nstore = new ArrayList<Double>(nfit); // may need to be new ArrayList(nfit) Collections.sort(nfit); int[] indexes = new int[nfit.size()]; for (int n = 0; n < nfit.size(); n++){ indexes[n] = nstore.indexOf(nfit.get(n)); } System.out.println(Arrays.toString(indexes)); 

If you need indexes in an ArrayList,

 Collections.sort(nstore); for (int n = 0; n < nfit.size(); nfit++){ nstore.add(n, nfit.indexOf(nstore.remove(n))); } Collections.sort(nfit); 

This will result in one sorted ArrayList nfit and one ArrayList of nstore indices.

+4
source

Make a copy of the ArrayList , then sort the copy. After that, use the indexOf method, passing the element of the sorted array and referencing the original ArrayList .

+1
source

If the values ​​are unique, you can use Map<Double, Integer> to store the value and the initial order. You just need to sort the card keys and get the corresponding value for each key.

+1
source

Two solutions: 1) if you are tied to an ArrayList, you will need to use Iterator to create an index list of the solution set. 2) Switch to the card with the value and order (which can be changed by sorting).

0
source

The following mergesort implementation implements the 'indexSort' method, which does this without interfering with the input array. http://algs4.cs.princeton.edu/22mergesort/Merge.java.html http://algs4.cs.princeton.edu/code/javadoc/Merge.html#indexSort (java.lang.Comparable [])

0
source

Bubble sorting is always your friend. You can use it to sort the array and at the same time keep track of the original indexes of the array. It will also work if there are several similar elements in your array.

 import java.util.*; import java.util.Arrays; import java.util.Random; public class BubbleSort { public static void main(String[] args) { Random rnd = new Random(); int count = 5; ArrayList<Double> array = new ArrayList<Double>(); int[] indices = new int[count]; for(int i = 0; i < indices.length; i++) { array.add(rnd.nextDouble() * count); indices[i] = i; } ArrayList<Double> values = new ArrayList<Double>(array); bubbleSort(values, indices); System.out.println("Unsorted input array :" + array); System.out.print("Sorted input array : "); for(int i = 0; i < array.size(); i++) { System.out.print(array.get(indices[i]) + " " ); } System.out.println(); } private static void bubbleSort(ArrayList<Double> values, int[] indices) { for(int k = 0; k < values.size(); k++) { for(int l = k + 1; l < values.size(); l++) { if(values.get(k) > values.get(l)) { double temp_value = values.get(k); values.set(k, values.get(l)); values.set(l, temp_value); int temp_index = indices[k]; indices[k] = indices[l]; indices[l] = temp_index; } } } } } 

The reason I answered this question after this many years is because the accepted answer will fail if you have several elements with the same values, because in indexes[n] = nstore.indexOf(nfit.get(n)); , indexOf always returns the first occurrence of the specified element, which leads to the creation of the wrong index array if there are similar elements in the input array.

Finally, copy and paste the code into BubbleSort.java , and then run javac BubbleSort.java && java BubbleSort to compile and view the results.

0
source

All Articles