Here you can find the source of sortedIndex(double[] values)
Parameter | Description |
---|---|
values | : to be sorted |
public static int[] sortedIndex(double[] values)
//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; } }