Java排序方法--List,数组,【自定义】继承Comparable和Comparator
(一)list和数组
①List默认排序代码:
public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("阿a里 "); list.add("不b安定 "); list.add("嘻x嘻 "); list.add("一y一 "); list.add("传c递 "); list.add("东d西 "); list.add("耳e朵 "); list.add("分f数 "); for (int i=0;i<list.size();i++){ System.out.print(list.get(i)); } //降序【以现有list的顺序----从后往前输出 】 System.out.println(" 降序"); Collections.reverse(list); for (int i=0;i<list.size();i++){ System.out.print(list.get(i)); } //依照list 现在顺序---从前往后读 一y一 不b安定 东d西 传c递 分f数 嘻x嘻 耳e朵 阿a里 System.out.println(" 升序"); Collections.sort(list); for (int i=0;i<list.size();i++){ System.out.print(list.get(i)); } System.out.println("按照汉语拼音排序"); //排序 abcdefg这么来的【首字母按照汉语拼音来的】 Collections.sort(list,Collator.getInstance(Locale.CHINA)); for (int i=0;i<list.size();i++){ System.out.print(list.get(i)); } }
②:数组:
String[] names = {"阿a里", "把b把", "阿a里", "得d到", "额e额", "得d到","阿b里"}; //应该是根据ASCII码这么来的 Arrays.sort(names); System.out.println(Arrays.toString(names)); Arrays.sort(names,Collator.getInstance(Locale.CHINA)); //根据中文字母表排序,先比较第一个字母拼音,再比较第二个字母拼音。 System.out.println(Arrays.toString(names));
(二)自定义排序:
①实现Comparable接口,类内设比较项。
public class Test1 implements Comparable { /* * 1.实现Comparable接口 * 重写int compareTo(Object o)方法 * */ private String name; private Integer age; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Test1(String name, Integer age) { this.name = name; this.age = age; } @Override public int compareTo(Object o) { Test1 sutdent = (Test1) o; int age1 = sutdent.getAge(); return this.age.compareTo(age1); } public static void main(String[] args) { List<Test1> students = new ArrayList<>(); Test1 s1 = new Test1("十一", 11); Test1 s2 = new Test1("十二", 12); Test1 s3 = new Test1("十三", 13); Test1 s4 = new Test1("十四", 14); students.add(s1); students.add(s4); students.add(s2); students.add(s3); students.add(s2); //reverse 仅仅只是反转 Collections.reverse(students); for (Test1 t : students) { System.out.print(t.getAge() + " "); } //调用自己的类的int属性进行比较 Collections.sort(students); for (Test1 t : students) { System.out.print(t.getAge() + " "); } System.out.println(" "); } }
②实现Comparator接口,类外设比较项。
public class Test1 implements Comparator<Test1> { /* * 1.实现Comparable接口 * 重写int compareTo(Object o)方法 * */ private String name; private Integer age; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Test1() { } public Test1(String name, Integer age) { this.name = name; this.age = age; } @Override public int compare(Test1 o1, Test1 o2) { return o1.age.compareTo(o2.age); } public static void main(String[] args) { Test1 s1=new Test1("小1",11); Test1 s3=new Test1("小3",13); Test1 s2=new Test1("小2",12); Test1 s4=new Test1("小4",14); //Array.adList就是固定长度集合。 List<Test1> students=Arrays.asList(s1,s3,s2,s4); for (Test1 s:students){ System.out.print(s.age+" "); } System.out.println("排序后"); Collections.sort(students,new Test1()); for (Test1 s:students){ System.out.print(s.age+" "); }
} }