package com.inmind.collections04; import java.util.ArrayList; import java.util.Collections; /* static > void sort(List list) 根据其元素的自然排序,按照升序排列指定的列表。 */ public class Demo14 { public static void main(String[] args) { //整数的自然排序,默认升序 ArrayList lists = new ArrayList<>(); Collections.addAll(lists, 111, 22, 32, 4, 555); System.out.println(lists); //对集合进行排序 Collections.sort(lists); System.out.println(lists); System.out.println("-------------------------------------"); //字符串的默认排序,按照字典排序,按ASCII码值 ArrayList strs = new ArrayList<>(); Collections.addAll(strs, "ab", "ba", "aa", "ca", "ac","bb"); System.out.println(strs); //对集合进行排序 Collections.sort(strs); System.out.println(strs); //自定义类的排序(Student) ArrayList students = new ArrayList<>(); students.add(new Student("张三1", 18,88)); students.add(new Student("张三2", 18,90)); students.add(new Student("张三3", 18,93)); students.add(new Student("张三4", 18,39)); System.out.println(students); //注意:Collections.sort排序时,传入的集合的内容,必须拥有自然排序功能(实现Comparable) Collections.sort(students); System.out.println(students); } }