在已经安装jdk和eclipse的电脑上,如果还没有安装的请先安装,打开eclipse,新建一个java 工程,名字任意,jre版本也任意,都行
新建一个Sort类,设置一下报名,和把自动生成main函数打钩
编写代码package com.test;import java.util.Arrays;import java.util.Comparator;public class Sort {public static void main(String[] args) {int[] num={12,45,1,3,8,6,9,5,0,12,45};Arrays.sort(num);//默认是从小到大System.out.println("从小到大");for (int numite : num) {System.out.print(numite+" ");}System.out.println();Integer[] num1={12,45,1,3,8,6,9,5,0,12,45};Arrays.sort(num1,new Comparator() {@Overridepublic int compare(Integer o1, Integer o2) {// TODO Auto-generated method stubreturn o2.compareTo(o1);}});System.out.println("从大到小");for (int numite : num1) {System.out.print(numite+" ");}System.out.println();}}
输出结果从小到大0 1 3 5 6 8 9 12 12 45 45从大到小45 45 12 12 9 8 6 5 3 1 0