简体   繁体   中英

sort 2d arraylist and get index java

I need to sort a two dimensional arrylist java and get the index of the sorted element. to do this I wrote this code 1. first I make a general class to sort the array element and get the original index of the sorted elements:

public static int[] Sort_Index(double[] arr){
            int[] indices = new int[arr.length];
                indices[0] = 0;
                for(int i=1;i<arr.length;i++){
                    int j=i;
                    for(;j>=1 && arr[j]<arr[j-1];j--){
                            double temp = arr[j];
                            arr[j] = arr[j-1];
                            indices[j]=indices[j-1];
                            arr[j-1] = temp;
                    }
                    indices[j]=i;
                }
                return indices;//indices of sorted elements
        }

then I used this loop to arrange the arraylist y

for(int i=0;i<Input.General_Inputs.Num_objectives;i++){
            double[] sort_y=new double[y.size()];
            for(int row=0;row<y.size();row++)
                sort_y[row]=y.get(row).get(Input.General_Inputs.Num+i);
            int[] sort_y_index=Sort_Index(sort_y);

        }
    }

the next step for me is to use this index to store the value in y arraylist to a new arraylist. But I think this is totally inefficient any better ideas?

What you can do is create a separate index structure which contains pointers to the data (in this case indexes) and just sort the index structure. The original data will remain intact.

Here is and example

public static void main(String[] args) {
    double[] data = new double[]{123.123, 345.345, -5, 10, -123.4};
    ArrayList<Integer> index = new ArrayList<>(data.length);
    for(int i = 0; i<data.length; i++) {
        index.add(i);
    }
    Collections.sort(index, new Comparator<Integer>() {

        @Override
        public int compare(Integer o1, Integer o2) {
            return Double.compare(data[o1], data[o2]);
            //notice that we are comparing elements of the array *data*,
            //but we are swapping inside array *index*
        }
    });
    for(int i = 0; i<index.size(); i++) {
        System.out.println(data[index.get(i)]);
    }
}

So you get sorted data and get to keep original indexes.

Performance wise this is not efficient at CPU level for small elements because of lot of memory jumping. You are better with creating a pair (index, data_element) and then just sort the whole pairs.

It is efficient when the objects that we are sorting are big objects.

You can create a class wrapping the original indices:

private static class ElementWithIndices<E> {
    private final E e;
    private final int i;
    private final int j;
    // + constructor, getters, setters
}

And then:

List<List<E>> list = // ...
List<List<ElementWithIndices<E>>> listWithIndices = convert(list);
Collections.sort(listWithIndices, myComparator); // compare on the Es
// listWithIndices now contains the sorted elements with their original indices

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM