Компаратор для строковых 2D-массивов работает по-другому при сравнении 2D-целочисленного массива [закрыто]JAVA

Программисты JAVA общаются здесь
Ответить Пред. темаСлед. тема
Anonymous
 Компаратор для строковых 2D-массивов работает по-другому при сравнении 2D-целочисленного массива [закрыто]

Сообщение Anonymous »


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
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «JAVA»