Java Array Sort sortByFirst(int[] array1, int[] array2)

Here you can find the source of sortByFirst(int[] array1, int[] array2)

Description

sort By First

License

Apache License

Declaration

public static int[][] sortByFirst(int[] array1, int[] array2) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2013 EMBL-EBI//from ww w  . j  a va  2s . com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

import java.util.Arrays;
import java.util.Comparator;

public class Main {
    private static Comparator<int[]> intArray_2_Comparator = new Comparator<int[]>() {

        @Override
        public int compare(int[] o1, int[] o2) {
            int result = o1[0] - o2[0];
            if (result != 0)
                return -result;

            return -(o1[1] - o2[1]);
        }
    };

    public static int[][] sortByFirst(int[] array1, int[] array2) {
        int[][] sorted = new int[array1.length][2];
        for (int i = 0; i < array1.length; i++) {
            sorted[i][0] = array1[i];
            sorted[i][1] = array2[i];
        }

        Arrays.sort(sorted, intArray_2_Comparator);

        int[][] result = new int[2][array1.length];
        for (int i = 0; i < array1.length; i++) {
            result[0][i] = sorted[i][0];
            result[1][i] = sorted[i][1];
        }

        return result;
    }
}

Related

  1. SortAndConcatenateStringArray(String StrArray[], String Separator)
  2. sortArray(double[] array)
  3. sortArray(int[] a)
  4. sortArray(int[] array)
  5. sortArrayAndReturnIndex(double[] p, String t)
  6. sortByIndex(final double[] data, int size)
  7. sortByIndex(int start, int end, int[] indexes, double[] values)
  8. sortByIndex(int start, int end, int[] indexes, double[] values)
  9. sortByLength(String[] proposals)