Java Array Sort sortAccording2(int[] ary, Integer[] sortOrder)

Here you can find the source of sortAccording2(int[] ary, Integer[] sortOrder)

Description

Sort given array according given sort order.

License

Apache License

Declaration

public static void sortAccording2(int[] ary, Integer[] sortOrder) 

Method Source Code

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

public class Main {
    /** Sort given array according given sort order. Sort is implemented in-place. */
    public static void sortAccording2(int[] ary, Integer[] sortOrder) {
        Integer[] so = sortOrder.clone(); // we are modifying sortOrder to preserve exchanges
        for (int i = 0; i < ary.length; i++) {
            int tmp = ary[i];
            int idx = so[i];
            ary[i] = ary[idx];//from  w w w  .ja  v  a2  s. c  o  m
            ary[idx] = tmp;
            for (int j = i; j < so.length; j++)
                if (so[j] == i) {
                    so[j] = idx;
                    break;
                }
        }
    }

    /** Sort given array according given sort order. Sort is implemented in-place. */
    public static void sortAccording2(boolean[] ary, Integer[] sortOrder) {
        Integer[] so = sortOrder.clone(); // we are modifying sortOrder to preserve exchanges
        for (int i = 0; i < ary.length; i++) {
            boolean tmp = ary[i];
            int idx = so[i];
            ary[i] = ary[idx];
            ary[idx] = tmp;
            for (int j = i; j < so.length; j++)
                if (so[j] == i) {
                    so[j] = idx;
                    break;
                }
        }
    }
}

Related

  1. sort3(String[] a, int x, int y, int z)
  2. sort3(String[] arr, float[] brr)
  3. sort5(Long[] data)
  4. sortableBytesToInt(byte[] encoded, int offset)
  5. sortAccording(float[] ary, Integer[] sortOrder)
  6. sortAlleleCount(int[] count)
  7. SortAndConcatenateStringArray(String StrArray[], String Separator)
  8. sortArray(double[] array)
  9. sortArray(int[] a)