Компаратор для строковых 2D-массивов работает по-другому при сравнении 2D-целочисленного массива [закрыто] ⇐ JAVA
Компаратор для строковых 2D-массивов работает по-другому при сравнении 2D-целочисленного массива [закрыто]
I have a 2D string array. It should be stored in such a way that it should be sorted in descending order of 2nd element in array and if there is a tie, lexicographically as per first element. My java code is as follows:
String arr[][] = new String[][] { {"/dir/dir/file1","12"}, {"/dir/file1", "23"}, {"/dir/dir/file2","3"},{"/dir/file2", "23"} }; Arrays.sort (arr, (a1,b1)-> a1[1]==b1[1] ? a1[0].compareTo(b1[0]) : b1[1].compareTo(a1[1])); The output is:
[/dir/dir/file2, 3] [/dir/file1, 23] [/dir/file2, 23] [/dir/dir/file1, 12] However, when I try the same code for an integer 2D-array for the above condition.
int a[][] = new int[][]{{3,5},{2,4},{4,6},{0,6}}; Arrays.sort(a,(a1,b1)-> a1[1]==b1[1]? a1[0]-b1[0]: b1[1]-a1[1]);` The output is:
[0, 6] [4, 6] [3, 5] [2, 4] I am really not sure what is wrong here. I would appreciate if someone could explain me the difference here.
Источник: https://stackoverflow.com/questions/780 ... eger-array
I have a 2D string array. It should be stored in such a way that it should be sorted in descending order of 2nd element in array and if there is a tie, lexicographically as per first element. My java code is as follows:
String arr[][] = new String[][] { {"/dir/dir/file1","12"}, {"/dir/file1", "23"}, {"/dir/dir/file2","3"},{"/dir/file2", "23"} }; Arrays.sort (arr, (a1,b1)-> a1[1]==b1[1] ? a1[0].compareTo(b1[0]) : b1[1].compareTo(a1[1])); The output is:
[/dir/dir/file2, 3] [/dir/file1, 23] [/dir/file2, 23] [/dir/dir/file1, 12] However, when I try the same code for an integer 2D-array for the above condition.
int a[][] = new int[][]{{3,5},{2,4},{4,6},{0,6}}; Arrays.sort(a,(a1,b1)-> a1[1]==b1[1]? a1[0]-b1[0]: b1[1]-a1[1]);` The output is:
[0, 6] [4, 6] [3, 5] [2, 4] I am really not sure what is wrong here. I would appreciate if someone could explain me the difference here.
Источник: https://stackoverflow.com/questions/780 ... eger-array
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Неожиданная оценка при сравнении сходного целочисленного и восьмиугольного значения
Anonymous » » в форуме Php - 0 Ответы
- 2 Просмотры
-
Последнее сообщение Anonymous
-
-
-
Array_diff() не учитывает ключи при сравнении значений двух плоских ассоциативных массивов
Anonymous » » в форуме Php - 0 Ответы
- 23 Просмотры
-
Последнее сообщение Anonymous
-