Sort the array through the Arrays.sort() method

1. The Arrays.sort() method is a quick sorting method . It is suitable for sorting with less than 47 elements . For specific sorting, please refer to the following code.

2. It can be used to sort the array from small to large and it can also be used to sort the array from large to small. The following explains the four sorting methods.

a) Sort Arrays.sort(num) from smallest to largest.

	Integer[] arr = new Integer[]{3,60,2,1,4,55,6,8,47,2,3,5};
	Arrays.sort(arr);
	System.out.println("从小到大排序:");
	for(int i =0;i<arr.length ;i++){
		System.out.print(arr[i]+" ");//1 2 2 3 3 4 5 6 8 47 55 60 
	}

b)  Sort from largest to smallest :

Arrays.sort(num, new Comparator<Integer>() {

            @Override
            public int compare(Integer o1, Integer o2) {
                // TODO Auto-generated method stub
                return o2-o1;
            }
        });

Integer[] arr = new Integer[]{3,60,2,1,4,55,6,8,47,2,3,5};
Arrays.sort(arr, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
		// TODO Auto-generated method stub
		return o2-o1;
	}
});
System.out.println("从大到小排序:");
for(int i =0;i<arr.length ;i++){
	System.out.print(arr[i]+" ");//60 55 47 8 6 5 4 3 3 2 2 1 
}

c) Sort from smallest to largest after the Nth position. Note that the subscript is not N: Arrays.sort(num,N,num.length);

Integer[] arr = new Integer[]{3,60,2,1,4,55,6,8,47,2,3,5};
Arrays.sort(arr,2,arr.length);
System.out.println("在第二位(不是数组下标为2)以后进行从小到大排序:");
for(int i =0;i<arr.length ;i++){
	System.out.print(arr[i]+" ");
}

d) Sort from largest to smallest at the Nth position, note that the subscript is not N:

Arrays.sort(num,N,num.length,new Comparator<Integer>() {

            @Override
            public int compare(Integer o1, Integer o2) {

                return o2-o1;
            }
        });

Integer[] arr = new Integer[]{3,60,2,1,4,55,6,8,47,2,3,5};
Arrays.sort(arr,5,arr.length,new Comparator<Integer>() {
 
@Override
public int compare(Integer o1, Integer o2) {
 
	return o2-o1;
}
});
System.out.println("在第五位(不是数组下标为5)以后进行从大到小排序:");
for(int i =0;i<arr.length ;i++){
	System.out.print(arr[i]+" ");
}
public class ArraySortTest {
	public static void main(String[] args) {
		Integer[] arr = new Integer[]{3,60,2,1,4,55,6,8,47,2,3,5};
		
		
		Arrays.sort(arr);
		System.out.println("从小到大排序:");
		for(int i =0;i<arr.length ;i++){
			System.out.print(arr[i]+" ");//1 2 2 3 3 4 5 6 8 47 55 60 
		}
		
		Arrays.sort(arr, new Comparator<Integer>() {

			@Override
			public int compare(Integer o1, Integer o2) {
				// TODO Auto-generated method stub
				return o2-o1;
			}
		});
		System.out.println("\n从大到小排序:");
		for(int i =0;i<arr.length ;i++){
			System.out.print(arr[i]+" ");//60 55 47 8 6 5 4 3 3 2 2 1 
		}
		Arrays.sort(arr,2,arr.length);
		System.out.println("\n在第二位(不是数组下标为2)以后进行从小到大排序:");
		for(int i =0;i<arr.length ;i++){
			System.out.print(arr[i]+" ");//60 55 1 2 2 3 3 4 5 6 8 47 
		}
		Arrays.sort(arr,5,arr.length,new Comparator<Integer>() {

			@Override
			public int compare(Integer o1, Integer o2) {

				return o2-o1;
			}
		});
		System.out.println("\n在第五位(不是数组下标为5)以后进行从大到小排序:");
		for(int i =0;i<arr.length ;i++){
			System.out.print(arr[i]+" ");//60 55 1 2 2 47 8 6 5 4 3 3 
		}

	}
}

 

Guess you like

Origin blog.csdn.net/qq_36138652/article/details/97045743
Recommended