forked from menault/TD4_DEV51_Qualite_Algo
td4
This commit is contained in:
35
SortND.java
Normal file
35
SortND.java
Normal file
@@ -0,0 +1,35 @@
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
|
||||
public class SortND {
|
||||
|
||||
public static int[][] sort2D(int[][] array) {
|
||||
// 1. Trier chaque ligne
|
||||
for (int[] row : array) {
|
||||
Arrays.sort(row);
|
||||
}
|
||||
|
||||
// 2. Trier les lignes par la somme de leurs éléments
|
||||
Arrays.sort(array, Comparator.comparingInt(SortND::sumRow));
|
||||
|
||||
return array;
|
||||
}
|
||||
|
||||
private static int sumRow(int[] row) {
|
||||
int sum = 0;
|
||||
for (int x : row) sum += x;
|
||||
return sum;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
int[][] array = {
|
||||
{9, 4, 5},
|
||||
{0, 3, 2},
|
||||
{4, 1, 3}
|
||||
};
|
||||
|
||||
int[][] sorted = sort2D(array);
|
||||
|
||||
System.out.println(Arrays.deepToString(sorted));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user