Java Array Sort sortIndexes(final T[] array)

Here you can find the source of sortIndexes(final T[] array)

Description

sort Indexes

License

Apache License

Declaration

public static <T extends Comparable<T>> Integer[] sortIndexes(final T[] array) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.Arrays;

import java.util.Comparator;

public class Main {
    public static <T extends Comparable<T>> Integer[] sortIndexes(final T[] array) {

        Integer[] idx = new Integer[array.length];

        for (int i = 0; i < array.length; i++)
            idx[i] = i;//  w w  w  .  j  a v a  2  s  .  c  o  m

        Arrays.sort(idx, new Comparator<Integer>() {
            public int compare(final Integer o1, final Integer o2) {
                return array[o1].compareTo(array[o2]);
            }
        });

        return idx;
    }

    /**
     * Similar to the "order" function in R. Returns the indices of the sorted array.
     * @param array Array whose order is calculated
     * @return Array of indices for the ordered vector. The first index is the minimum value, and so forth.
     */
    public static Integer[] sortIndexes(final float[] array) {

        Integer[] idx = new Integer[array.length];

        for (int i = 0; i < array.length; i++)
            idx[i] = i;

        Arrays.sort(idx, new Comparator<Integer>() {
            public int compare(final Integer o1, final Integer o2) {
                return Float.compare(array[o1], array[o2]);
            }
        });

        return idx;
    }
}

Related

  1. sortedPointersInto_tryingToImproveSpeed(final double d[])
  2. sortedPointersInto_usingStrictfp(final double d[])
  3. sortEigenValues(int n, double[] d, double[][] v)
  4. sortIndex(double[] A)
  5. sortIndex(double[] doubleArray)
  6. sortIndexesDescending(final double[] in)
  7. sortindices(double[] x)
  8. sortIndices(float[] main)
  9. sortInPlace(final double[] v, final int i, final int j)