在实际编码的过程中,经常需要对数组进行排序,下面一起和大家看看几种常见的排序场景。
1.根据数字大小升序
Arrays.sort() 默认根据数字大小进行升序。
public class SortArray {
public static void main(String[] args) {
Integer[] ints = {12, 94, 75, 24, 14, 17, 9, 64, 32, 21, 30, 54, 27};
Arrays.sort(ints);
System.out.println(Arrays.toString(ints));
}
}
[9, 12, 14, 17, 21, 24, 27, 30, 32, 54, 64, 75, 94]
2.根据数字大小降序
Arrays.sort() 可以额外指定 JDK 原生的 Collections.reverseOrder() 为参数,来实现根据数字大小降序。
public class SortArray {
public static void main(String[] args) {
Integer[] ints = {12, 94, 75, 24, 14, 17, 9, 64, 32, 21, 30, 54, 27};
Arrays.sort(ints, Collections.reverseOrder());
System.out.println(Arrays.toString(ints));
}
}
[94, 75, 64, 54, 32, 30, 27, 24, 21, 17, 14, 12, 9]
3.字符串排序
我们来看看使用 Arrays.sort() 对字符串进行排序会产生什么样的结果呢?
public class StringArraySort {
public static void main(String[] args) {
String[] strings = {"Banana", "apple", "Pear", "orange", "WaterMelon", "peach"};
Arrays.sort(strings);
System.out.println(Arrays.toString(strings));
}
}
[Banana, Pear, WaterMelon, apple, orange, peach]
从打印结果可以发现,默认将首字母为大写的排列在前,小写在后,再依次根据字母的顺序进行升序。
不知道你是否会和我一样,对此种排序方式感到不自在,希望忽略大小写进行排序,可以在使用 Arrays.sort() 时额外指定 String.CASE_INSENSITIVE_ORDER 作为参数。
public class StringArraySort {
public static void main(String[] args) {
String[] strings = {"Banana", "apple", "Pear", "orange", "WaterMelon", "peach"};
Arrays.sort(strings, String.CASE_INSENSITIVE_ORDER);
System.out.println(Arrays.toString(strings));
}
}
[apple, Banana, orange, peach, Pear, WaterMelon]
4.自定义排序
假设现在需要排序的元素不是基本类型,该如何处理呢?
只需该类型实现 Comparable 接口中的 compareTo(T t) 方法,定义该对象与传入对象两者之间比较方式:若传入对象比本对象大,则返回正值,传入对象比本对象小,则返回负值,两者相等则返回 0 。再通过 Arrays.sort() 方法则会将数组内元素根据升序进行排列。
public class Student implements Comparable<Student> {
private int code;
private int age;
public Student(int code, int age) {
this.code = code;
this.age = age;
}
public int getCode() {
return code;
}
public int getAge() {
return age;
}
@Override
public int compareTo(Student stu) {
if (this.code > stu.getCode()) return 1;
if (this.code < stu.getCode()) return -1;
return 0;
}
@Override
public String toString() {
return "Student{" +
"code=" + code +
", age=" + age +
'}';
}
}
public class StudentArraySort {
public static void main(String[] args) {
Student[] students =
{new Student(1, 12), new Student(3, 9), new Student(2, 8), new Student(5, 10), new Student(4, 11)};
Arrays.sort(students);
System.out.println(Arrays.toString(students));
}
}
[Student{code=1, age=12}, Student{code=2, age=8}, Student{code=3, age=9}, Student{code=4, age=11}, Student{code=5, age=10}]
5.覆盖已定义的排序
倘若我们需要进行排序的类型没有实现 Comparable 接口,或者你并不喜欢它的排序方式,这时又该如何操作呢?
我们还是来上述 Student 为例,Student 实现 Comparable 接口,默认根据 code 升序,现在想改为根据 age 进行升序。
可以通过实现 Comparator 接口中的 compare() 方法对元素进行排序, 实现方式与 Comparable 中的方式基本一致,第一个参数为对照元素,第二个参数为传入进行比较的元素。
public class StudentAgeComparable implements Comparator<Student> {
@Override
public int compare(Student s1, Student s2) {
if (s1.getAge() > s2.getAge()) return 1;
if (s1.getAge() < s2.getAge()) return -1;
return 0;
}
}
public class StudentArraySort {
public static void main(String[] args) {
Student[] students =
{new Student(1, 12), new Student(3, 9), new Student(2, 8), new Student(5, 10), new Student(4, 11)};
Arrays.sort(students, new StudentAgeComparator());
System.out.println(Arrays.toString(students));
}
}
[Student{code=2, age=8}, Student{code=3, age=9}, Student{code=5, age=10}, Student{code=4, age=11}, Student{code=1, age=12}]
本次分享至此结束,希望本文对你有所帮助,若能点亮下方的点赞按钮,在下感激不尽,谢谢您的【精神支持】。
若有任何疑问,也欢迎与我交流,若存在不足之处,也欢迎各位指正!