This method is a static method of the Arrays class, used to sort arrays. The time complexity is O(n*logn), and the method return value is void. After sorting, the sorted results are stored in the array. Since this method performs ascending sorting based on the space of the original array, there is no need to define an array to receive it, that is, no return value is required.
Arrays.sort() overloads four types of methods:
Sort the specified T-shaped array in ascending numerical order, for example:
importjava.util.Arrays;importjava.util.Comparator;publicclassMain{publicstaticvoidmain(String[]args){int[]a={2,5,1,8,12};Arrays.sort(a);System.out. println(Arrays.toString(a));}}
The running results are as follows:
[1,2,5,8,12]
Sort the specified range of the specified T-shaped array in ascending numerical order, for example:
importjava.util.Arrays;importjava.util.Comparator;publicclassMain{publicstaticvoidmain(String[]args){int[]a={8,2,15,4,7,1};Arrays.sort(a,1,5 );System.out.println(Arrays.toString(a));}}
The running results are as follows:
[8,2,4,7,15,1]
Sorts a T-shaped array according to the order produced by the specified comparator.
importjava.util.Arrays;importjava.util.Comparator;publicclassMain{publicstaticvoidmain(String[]args){int[][]nums=newint[][]{{1,3},{5,7},{9, 5},{7,3}};Arrays.sort(nums,newComparator<int[]>(){publicintcompare(int[]a,int[]b){if(a[0]==b[0] ){returna[1]-b[1];}else{returna[0]-b[0];}}});for(int[]num:nums)System.out.println(Arrays.toString(num ));int[]a={8,2,15,4,7,1};Arrays.sort(a,1,5);System.out.println(Arrays.toString(a));}}
The running results are as follows:
[1,3][5,7][7,3][9,5][8,2,4,7,15,1]
importjava.util.Arrays;importjava.util.Comparator;publicclassMain{publicstaticvoidmain(String[]args){int[][]nums=newint[][]{{1,3},{5,7},{9, 5},{7,3}};Arrays.sort(nums,newComparator<int[]>(){publicintcompare(int[]a,int[]b){if(a[1]==b[1] ){returna[0]-b[0];}else{returna[1]-b[1];}}});for(int[]num:nums)System.out.println(Arrays.toString(num ));}}
The running results are as follows:
[1,3][7,3][9,5][5,7]
Sort the specified range of the T-type array according to the order produced by the specified comparator, for example:
importjava.util.Arrays;importjava.util.Comparator;publicclassMain{publicstaticvoidmain(String[]args){int[][]nums=newint[][]{{9,5},{7,3},{5, 7},{1,3}};Arrays.sort(nums,1,4,newComparator<int[]>(){publicintcompare(int[]a,int[]b){if(a[0]== b[0]){returna[1]-b[1];}else{returna[0]-b[0];}}});for(int[]num:nums)System.out.println(Arrays .toString(num));}}
The running results are as follows:
[9,5][1,3][5,7][7,3]