Java Array Sort sortedIndex(double[] values)

Here you can find the source of sortedIndex(double[] values)

Description

Sort an array and return the result as an array of indices

License

Open Source License

Parameter

Parameter Description
values : to be sorted

Return

indices : first value points to smallest value and last one to largest

Declaration

public static int[] sortedIndex(double[] values) 

Method Source Code

//package com.java2s;
/* MOD_V2.0//  ww  w.  j  a v a2 s  . c  o m
 * Copyright (c) 2012 OpenDA Association
 * All rights reserved.
 * 
 * This file is part of OpenDA. 
 * 
 * OpenDA is free software: you can redistribute it and/or modify 
 * it under the terms of the GNU Lesser General Public License as 
 * published by the Free Software Foundation, either version 3 of 
 * the License, or (at your option) any later version. 
 * 
 * OpenDA is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 * GNU Lesser General Public License for more details. 
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with OpenDA.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * Sort an array and return the result as an array of indices
     * @param values : to be sorted
     * @return indices : first value points to smallest value and last one to largest
     */
    public static int[] sortedIndex(double[] values) {

        class indexValuePair {
            int index;
            double value;

            public indexValuePair(int index, double value) {
                this.index = index;
                this.value = value;
            }
        }
        class ValueComparator implements java.util.Comparator<indexValuePair> {
            public int compare(indexValuePair o1, indexValuePair o2) {
                return new Double(((indexValuePair) o1).value).compareTo(new Double(((indexValuePair) o2).value));
            }
        }

        int[] result = new int[values.length];
        java.util.ArrayList<indexValuePair> sortedValues = new java.util.ArrayList<indexValuePair>();
        for (int i = 0; i < values.length; i++) {
            sortedValues.add(new indexValuePair(i, values[i]));
        }
        ValueComparator vc = new ValueComparator();
        java.util.Collections.sort(sortedValues, vc);
        int j = 0;
        for (indexValuePair ivp : sortedValues) {
            result[j] = ivp.index;
            j++;
        }
        return result;
    }
}

Related

  1. sortDescending(T[] a, int fromIndex, int toIndex)
  2. SortDoubleDimensionArray(String StrArr[][])
  3. sorted(int[] array)
  4. sortedArraysEqual(Object[] arr1, Object[] arr2)
  5. sortedCopyOf(final int[] array)
  6. sortedIndexOf(Comparable[] list, Comparable val, int lower, int higher)
  7. sortedMerge(int[] a, int[] b)
  8. sortedMerge(int[] aIds, double[] aVals, int[] bIds, double[] bVals, int[] resIds, double[] resVals)
  9. sortedPointersInto(double d[])