Here you can find the source of sortByFirst(int[] array1, int[] array2)
public static int[][] sortByFirst(int[] array1, int[] array2)
//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; } }