网课上的排序没有中文的,查了一下,大概得这么写:
package DemoCollections; import java.text.Collator; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Locale; public class DemoCollectons01 { public static void main(String[] args) { ArrayList<String> list = new ArrayList<>(); Collections.addAll(list,"what","the","Fuck"); Collections.shuffle(list); System.out.println(list); Collections.sort(list); System.out.println(list); System.out.println("======================="); ArrayList<Person> list1 = new ArrayList<>(); list1.add(new Person(8,"朱柏澍")); list1.add(new Person(9,"潘卓成")); list1.add(new Person(55,"王奎智")); list1.add(new Person(1,"古力娜扎")); // Collections.sort(list1); System.out.println(list1); Collections.sort(list1, new Comparator<Person>() { @Override public int compare(Person o1, Person o2) { Comparator<Object> comparator = Collator.getInstance(Locale.CHINA); return comparator.compare(o1.getName(),o2.getName()); } }); System.out.println(list1); } }
参考:JAVA中文排序