Java - Sort one array based on values of another array?

后端 未结 10 1822
不思量自难忘°
不思量自难忘° 2020-11-30 12:02

I have an array of Strings that are instances of a class from external code that I would rather not change.

I also have an array of ints that was generated by callin

10条回答
  •  甜味超标
    2020-11-30 12:24

    I had a similar problem, and solved it by coding a sorting algorithm which sorted an array of measures, and made identical swaps in the array of objects. Here is the code, with tests, best wishes and no promises:

    package other;
    
    import java.util.Arrays;
    import java.util.Random;
    
    /**
     * Sorts an array of objects (bags) by a separate array of doubles (measures). 
     * It sorts into ascending order. 
     * 

    * The results array is always a new array. *

    * The algorithm used:

      *
    • Is (I believe) a merge-sort, which would mean it is stable. (I haven't tested this.) *
    • Efficiently exploits already ordered subsequences. *
    • Requires the allocation of eight arrays: four of the baggage type, four of doubles, each the length of the original data. *
    *

    * A NaN in the measures - I haven't thought about that, and don't want to. *

    * There is test code at the end of the class. */ public class SortBaggageByDouble { public final Object [] results ; protected final int length ; public SortBaggageByDouble(Object[] bags, double[] measures) { this.length = bags.length; if (bags.length!=measures.length) throw new IllegalArgumentException("Mismatched lengths: payload array "+bags.length+", measures array "+measures.length); this.results = new Object[length]; Object [] bagsA = new Object[length] ; Object [] bagsB = new Object[length] ; Object [] bagsC = new Object[length] ; Object [] bagsD = new Object[length] ; double [] measuresA = new double[length] ; double [] measuresB = new double[length] ; double [] measuresC = new double[length] ; double [] measuresD = new double[length] ; System.arraycopy(bags, 0, bagsA, 0, length); System.arraycopy(measures, 0, measuresA, 0, length); munge(length, 0, bagsA, bagsB, bagsC, bagsD, measuresA, measuresB, measuresC, measuresD); } private void munge(int inLengthA, int inLengthB, Object[] inBagsA, Object[] inBagsB, Object[] outBagsC, Object[] outBagsD, double[] inMeasuresA, double[] inMeasuresB, double[] outMeasuresC, double[] outMeasuresD) { int outLengthC = 0 ; int outLengthD = 0 ; int cursorA = 0 ; int cursorB = 0 ; boolean toC = true ; while(outLengthC+outLengthD=inLengthA) { fromA = false ; } else if (cursorB>=inLengthB) { fromA = true ; } else { fromA = inMeasuresA[cursorA] <= inMeasuresB[cursorB] ; } double tmpMeasure = fromA ? inMeasuresA[cursorA] : inMeasuresB[cursorB] ; Object tmpBag = fromA ? inBagsA[cursorA] : inBagsB[cursorB] ; if (fromA) cursorA ++ ; else cursorB ++ ; if (toC) { if (outLengthC==0 || (outMeasuresC[outLengthC-1]<=tmpMeasure)) { outMeasuresC[outLengthC] = tmpMeasure ; outBagsC[outLengthC] = tmpBag ; outLengthC ++ ; } else { toC = false ; outMeasuresD[outLengthD] = tmpMeasure ; outBagsD[outLengthD] = tmpBag ; outLengthD ++ ; } } else { if (outLengthD==0 || (outMeasuresD[outLengthD-1]<=tmpMeasure)) { outMeasuresD[outLengthD] = tmpMeasure ; outBagsD[outLengthD] = tmpBag ; outLengthD ++ ; } else { toC = true ; outMeasuresC[outLengthC] = tmpMeasure ; outBagsC[outLengthC] = tmpBag ; outLengthC ++ ; } } } if (outLengthC==length) { System.arraycopy(outBagsC, 0, results, 0, length); } else { munge(outLengthC, outLengthD, outBagsC, outBagsD, inBagsA, inBagsB, outMeasuresC, outMeasuresD, inMeasuresA, inMeasuresB); } } /** * Subclass to sort strings, with a result object sortedStrings which is of a useful type. */ public static class Strings extends SortBaggageByDouble { public final String [] sortedStrings ; public Strings(String[] in, double[] measures) { super(in, measures); this.sortedStrings = new String[results.length]; for (int i=0 ; i0) sb.append(' '); sb.append(array[i]); } } public static String stringfor(double[] array) { StringBuilder sb = new StringBuilder(); build(sb, array); return sb.toString(); } public static void build(StringBuilder sb, double[] array) { for (int i=0 ; i0) sb.append(' '); sb.append(array[i]); } } public static String stringfor(String[] labels) { StringBuffer sb = new StringBuffer(); String sep = "" ; for (int i=0 ; i

提交回复
热议问题