Java中的数组排序,一般是利用Arrays.sort(),这个方法是经过优化的快速排序。在Arrays种有多中形式的重载,在这里就不一一列举了。
数组排序的种类:
1.非降序排序, 非升序排序(就排序后数组元素排列的结果而言)
2.基本类型数据的排序,类类型数据的排序(就排序的对象而言)
排序示例:
int型数组的非降序排序:
package sort; import java.util.Arrays; public class Main {
public static void displayArray(int[] array) {
for (int i: array) {
System.out.print(i + " ");
}
System.out.println();
} public static void main(String[] args) {
int[] arr = new int[]{43, 84, 3, 8, 4, 7, 3, 75, 82, 748, 35}; System.out.println("排序前:");
displayArray(arr); Arrays.sort(arr); System.out.println("排序后:");
displayArray(arr);
}
}
运行结果如下:
int型数组的非升序排序:
package sort; import java.util.Arrays;
import java.util.Comparator; public class Main {
public static void displayArray(Integer[] array) {
for (int i: array) {
System.out.print(i + " ");
}
System.out.println();
} public static void main(String[] args) {
Integer[] arr = new Integer[]{43, 84, 3, 8, 4, 7, 3, 75, 82, 748, 35}; System.out.println("排序前:");
displayArray(arr); Arrays.sort(arr, new JX()); System.out.println("排序后:");
displayArray(arr);
}
} class JX implements Comparator<Integer> {
@Override
public int compare(Integer o1, Integer o2) {
if (o1 <= o2) {
return 1;
}
return -1;
}
}
运行结果如下:
类类型的非降序排序:
package sort; import java.util.Arrays; public class Main {
public static void displayArray(Student[] student) {
for (Student s : student) {
System.out.println(s);
}
} public static void main(String[] args) {
Student[] s = new Student[5];
s[0] = new Student("wwww", 20, 2.34);
s[1] = new Student("kkkk", 2, 2.34);
s[2] = new Student("pppp", 25, 3.34);
s[3] = new Student("hhhh", 12, 4.34);
s[4] = new Student("jjjj", 10, 5.34); System.out.println("排序前:");
displayArray(s); Arrays.sort(s); System.out.println("排序后:");
displayArray(s);
}
} /*
* 根据年龄进行非降序排序
*/
class Student implements Comparable<Student> {
private String name;
private int age;
private double height; public Student(String name, int age, double height) {
this.name = name;
this.age = age;
this.height = height;
} @Override
public int compareTo(Student o) {
if (this.age <= o.age) {
return -1;
}
return 1;
} public String toString() {
return "Name: " + name + " Age: " + age + " Height: " + height;
}
}
运行结果如下:
类类型的非升序排序:
package sort; import java.util.Arrays; public class Main {
public static void displayArray(Student[] student) {
for (Student s : student) {
System.out.println(s);
}
} public static void main(String[] args) {
Student[] s = new Student[5];
s[0] = new Student("wwww", 20, 2.34);
s[1] = new Student("kkkk", 2, 2.34);
s[2] = new Student("pppp", 25, 3.34);
s[3] = new Student("hhhh", 12, 4.34);
s[4] = new Student("jjjj", 10, 5.34); System.out.println("排序前:");
displayArray(s); Arrays.sort(s); System.out.println("排序后:");
displayArray(s);
}
} /*
* 根据年龄进行非升序排序
*/
class Student implements Comparable<Student> {
private String name;
private int age;
private double height; public Student(String name, int age, double height) {
this.name = name;
this.age = age;
this.height = height;
} @Override
public int compareTo(Student o) {
if (this.age <= o.age) {
return 1;
}
return -1;
} public String toString() {
return "Name: " + name + " Age: " + age + " Height: " + height;
}
}
运行结果如下:
根据指定属性对类类型数组排序:
package sort; import java.util.Arrays;
import java.util.Comparator; public class Main {
public static void displayArray(Student[] student) {
for (Student s : student) {
System.out.println(s);
}
} public static void main(String[] args) {
Student[] s = new Student[5];
s[0] = new Student("wwww", 20, 2.34);
s[1] = new Student("kkkk", 2, 2.34);
s[2] = new Student("pppp", 25, 3.34);
s[3] = new Student("hhhh", 12, 4.34);
s[4] = new Student("jjjj", 10, 5.34);
/*
System.out.println("排序前:");
displayArray(s);
*/
System.out.println("按age进行非降序排序");
Arrays.sort(s, new SortByAge());
displayArray(s); System.out.println("按height进行非升序排序");
Arrays.sort(s, new SortByHeight());
displayArray(s);
}
} class Student {
private String name;
private int age;
private double height; public Student(String name, int age, double height) {
this.name = name;
this.age = age;
this.height = height;
} public String toString() {
return "Name: " + name + " Age: " + age + " Height: " + height;
} public int getAge() {
return age;
} public double getHeight() {
return height;
}
} /*
* 按age进行非降序排序
*/
class SortByAge implements Comparator<Student> {
@Override
public int compare(Student o1, Student o2) {
if (o1.getAge() <= o2.getAge()) {
return -1;
}
return 1;
}
} /*
* 按height进行非升序排序
*/
class SortByHeight implements Comparator<Student> {
@Override
public int compare(Student o1, Student o2) {
if (o1.getHeight() - o2.getHeight() < 0.01) {
return 1;
}
return -1;
} }
运行结果如下: