简体   繁体   中英

How should java comparator class be declared to sort the array by there second element in ascending and descending order

int arr[][] = new int[][] {
            {3, 7},
            {1, 2},
            {8, 9},
            {4, 2},
            {5, 3},
            {0,90}
        };

this is my array ,i can sort it ascending or descending order based on there first element using bellow code :

Arrays.sort(arr,(a,b)->Integer.compare(a[0], b[0]));

Now my question is how to do the same thing using second element on the array.

Instead of accessing the first element with index 0 you want to access the second element using index 1 , ie, a[1] and b[1] .

I suggest you should read up on how to use arrays , since this question is not specific to sorting but shows a lack of understanding arrays in general.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM