在平時開發的過程當中,常常會遇到須要對數組、集合中元素按規則進行排序,本文記錄在開發過程當中可能遇到的狀況以及相關的完整代碼示例。java
1.1 數組排序用法git
String[] strArr = new String[]{"zhangsan","lisi","wangwu"}; //數組默認按字符升序排序 Arrays.sort(strArr); System.out.println("默認按字母升序排序:"); for (String str:strArr) { System.out.println(str); }
1.2 集合排序用法github
List<String> strList = new ArrayList<>(); strList.add("zhangsan"); strList.add("lisi"); strList.add("wangwu"); //集合默認按字符升序排序 Collections.sort(strList); System.out.println("默認按字母升序排序:"); for (String str:strList) { System.out.println(str); }
package com.simon.interfacedemo.sortdemo.stringdemo; import java.util.Comparator; /** * @Description: 經過實現Comparator接口,實現自定義排序 */ public class StringComparator implements Comparator<String>{ /** * 按字符串長度降序排序 */ @Override public int compare(String o1, String o2) { return o1.length() > o2.length() ? -1 : 1; } }
2.1 數組使用StringComparator排序器數組
String[] strArr = new String[]{"zhangsan","lisi","wangwu"}; //自定義排序,按字符串長度升序 Arrays.sort(strArr,new StringComparator()); System.out.println("自定義排序,按字符串長度降序排序:"); for (String str:strArr) { System.out.println(str); }
2.2 集合使用StringComparator排序器ide
List<String> strList = new ArrayList<>(); strList.add("zhangsan"); strList.add("lisi"); strList.add("wangwu"); //自定義排序,按字符串長度降序 Collections.sort(strList,new StringComparator()); System.out.println("自定義排序,按字符串長度降序排序:"); for (String str:strList) { System.out.println(str); }
public class Student implements Comparable<Student>{ private String name; private Integer age; public Student(String name,Integer age){ this.name = name; this.age = age; } public String getName() { return name; } /** * 實現compareTo接口方法,按age升序。 * @param o * @return 返回1:大於,0:等於,-1:小於 */ @Override public int compareTo(Student o) { return Integer.compare(age,o.age); } /** * 重寫toString方法,方便System.out.println打印出詳細的信息。 */ @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
3.1 Student數組按age升序排序this
Student[] students = new Student[3]; students[0] = new Student("zhangsan",30); students[1] = new Student("lisi",28); students[2] = new Student("wangwu",33); System.out.println("經過student實現的默認排序,根據age升序排序:"); Arrays.sort(students); for (Student student : students) { System.out.println(student); }
3.2 Student集合按age升序排序code
List<Student> studentList = new ArrayList<>(); studentList.add(new Student("zhangsan",30)); studentList.add(new Student("lisi",28)); studentList.add(new Student("wangwu",33)); System.out.println("經過student實現的默認排序,根據age升序排序:"); Collections.sort(studentList); for (Student student : studentList) { System.out.println(student); }
package com.simon.interfacedemo.sortdemo.studentdemo; import java.util.Comparator; /** * @Description: 經過實現Comparator接口,實現自定義排序 */ public class StudentComparator implements Comparator<Student>{ /** * 按名字長度升序排序 */ @Override public int compare(Student o1, Student o2) { return o1.getName().length() > o2.getName().length() ? 1 : -1; } }
4.1 數組使用StudentComparator排序器對象
Student[] students = new Student[3]; students[0] = new Student("zhangsan",30); students[1] = new Student("lisi",28); students[2] = new Student("wangwu",33); System.out.println("經過自定義排序器實現的排序,根據名字長度排序:"); Arrays.sort(students,new StudentComparator()); for (Student student : students) { System.out.println(student); }
4.2 集合使用StudentComparator排序器blog
List<Student> studentList = new ArrayList<>(); studentList.add(new Student("zhangsan",30)); studentList.add(new Student("lisi",28)); studentList.add(new Student("wangwu",33)); System.out.println("經過自定義排序器實現的排序,根據名字長度排序:"); Collections.sort(studentList,new StudentComparator()); for (Student student : students) { System.out.println(student); }