Java中的数组排序

Java中的数组排序,一般是利用Arrays.sort(),这个方法是经过优化的快速排序。在Arrays种有多中形式的重载,在这里就不一一列举了。

数组排序的种类:

1.非降序排序, 非升序排序(就排序后数组元素排列的结果而言)

2.基本类型数据的排序,类类型数据的排序(就排序的对象而言)

 

排序示例:

int型数组的非降序排序:

 1 package sort;

 2 

 3 import java.util.Arrays;

 4 

 5 public class Main {

 6     public static void displayArray(int[] array) {

 7         for (int i: array) {

 8             System.out.print(i + " ");

 9         }

10         System.out.println();

11     }

12     

13     public static void main(String[] args) {

14         int[] arr = new int[]{43, 84, 3, 8, 4, 7, 3, 75, 82, 748, 35};

15         

16         System.out.println("排序前:");

17         displayArray(arr);

18         

19         Arrays.sort(arr);

20         

21         System.out.println("排序后:");

22         displayArray(arr);

23     }

24 }
View Code

运行结果如下:

Java中的数组排序

 

int型数组的非升序排序:

 1 package sort;

 2 

 3 import java.util.Arrays;

 4 import java.util.Comparator;

 5 

 6 public class Main {

 7     public static void displayArray(Integer[] array) {

 8         for (int i: array) {

 9             System.out.print(i + " ");

10         }

11         System.out.println();

12     }

13     

14     public static void main(String[] args) {

15         Integer[] arr = new Integer[]{43, 84, 3, 8, 4, 7, 3, 75, 82, 748, 35};

16         

17         System.out.println("排序前:");

18         displayArray(arr);

19         

20         Arrays.sort(arr, new JX());

21         

22         System.out.println("排序后:");

23         displayArray(arr);

24     }

25 }

26 

27 class JX implements Comparator<Integer> {

28     @Override

29     public int compare(Integer o1, Integer o2) {

30         if (o1 <= o2) {

31             return 1;

32         }

33         return -1;

34     }

35 }
View Code

运行结果如下:

Java中的数组排序

类类型的非降序排序:

 1 package sort;

 2 

 3 import java.util.Arrays;

 4 

 5 public class Main {

 6     public static void displayArray(Student[] student) {

 7         for (Student s : student) {

 8             System.out.println(s);

 9         }

10     }

11     

12     public static void main(String[] args) {

13         Student[] s = new Student[5];

14         s[0] = new Student("wwww", 20, 2.34);

15         s[1] = new Student("kkkk", 2, 2.34);

16         s[2] = new Student("pppp", 25, 3.34);

17         s[3] = new Student("hhhh", 12, 4.34);

18         s[4] = new Student("jjjj", 10, 5.34);

19         

20         System.out.println("排序前:");

21         displayArray(s);

22         

23         Arrays.sort(s);

24         

25         System.out.println("排序后:");

26         displayArray(s);

27     }

28 }

29 

30 /*

31  * 根据年龄进行非降序排序

32  */

33 class Student implements Comparable<Student> {

34     private String name;

35     private int age;

36     private double height;

37     

38     public Student(String name, int age, double height) {

39         this.name = name;

40         this.age = age;

41         this.height = height;

42     }

43     

44     @Override

45     public int compareTo(Student o) {

46         if (this.age <= o.age) {

47             return -1;

48         }

49         return 1;

50     }

51     

52     public String toString() {

53         return "Name: " + name + " Age: " + age + " Height: " + height;

54     }

55 }
View Code

运行结果如下:

Java中的数组排序

类类型的非升序排序:

 1 package sort;

 2 

 3 import java.util.Arrays;

 4 

 5 public class Main {

 6     public static void displayArray(Student[] student) {

 7         for (Student s : student) {

 8             System.out.println(s);

 9         }

10     }

11     

12     public static void main(String[] args) {

13         Student[] s = new Student[5];

14         s[0] = new Student("wwww", 20, 2.34);

15         s[1] = new Student("kkkk", 2, 2.34);

16         s[2] = new Student("pppp", 25, 3.34);

17         s[3] = new Student("hhhh", 12, 4.34);

18         s[4] = new Student("jjjj", 10, 5.34);

19         

20         System.out.println("排序前:");

21         displayArray(s);

22         

23         Arrays.sort(s);

24         

25         System.out.println("排序后:");

26         displayArray(s);

27     }

28 }

29 

30 /*

31  * 根据年龄进行非升序排序

32  */

33 class Student implements Comparable<Student> {

34     private String name;

35     private int age;

36     private double height;

37     

38     public Student(String name, int age, double height) {

39         this.name = name;

40         this.age = age;

41         this.height = height;

42     }

43     

44     @Override

45     public int compareTo(Student o) {

46         if (this.age <= o.age) {

47             return 1;

48         }

49         return -1;

50     }

51     

52     public String toString() {

53         return "Name: " + name + " Age: " + age + " Height: " + height;

54     }

55 }
View Code

运行结果如下:

Java中的数组排序

根据指定属性对类类型数组排序:

 1 package sort;

 2 

 3 import java.util.Arrays;

 4 import java.util.Comparator;

 5 

 6 public class Main {

 7     public static void displayArray(Student[] student) {

 8         for (Student s : student) {

 9             System.out.println(s);

10         }

11     }

12     

13     public static void main(String[] args) {

14         Student[] s = new Student[5];

15         s[0] = new Student("wwww", 20, 2.34);

16         s[1] = new Student("kkkk", 2, 2.34);

17         s[2] = new Student("pppp", 25, 3.34);

18         s[3] = new Student("hhhh", 12, 4.34);

19         s[4] = new Student("jjjj", 10, 5.34);

20         /*

21         System.out.println("排序前:");

22         displayArray(s);

23         */

24         System.out.println("按age进行非降序排序");

25         Arrays.sort(s, new SortByAge());

26         displayArray(s);

27         

28         System.out.println("按height进行非升序排序");

29         Arrays.sort(s, new SortByHeight());

30         displayArray(s);

31     }

32 }

33 

34 class Student {

35     private String name;

36     private int age;

37     private double height;

38     

39     public Student(String name, int age, double height) {

40         this.name = name;

41         this.age = age;

42         this.height = height;

43     }

44     

45     public String toString() {

46         return "Name: " + name + " Age: " + age + " Height: " + height;

47     }

48     

49     public int getAge() {

50         return age;

51     }

52     

53     public double getHeight() {

54         return height;

55     }

56 }

57 

58 /*

59  * 按age进行非降序排序

60  */

61 class SortByAge implements Comparator<Student> {

62     @Override

63     public int compare(Student o1, Student o2) {

64         if (o1.getAge() <= o2.getAge()) {

65             return -1;

66         }

67         return 1;

68     }

69 }

70 

71 /*

72  * 按height进行非升序排序

73  */

74 class SortByHeight implements Comparator<Student> {

75     @Override

76     public int compare(Student o1, Student o2) {

77         if (o1.getHeight() - o2.getHeight() < 0.01) {

78             return 1;

79         }

80         return -1;

81     }

82     

83 }
View Code

运行结果如下:

Java中的数组排序

 

你可能感兴趣的:(java)