1. 程式人生 > >Java實現排序的方法--List,陣列排序。【自定義】繼承Comparable和Comparator

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+"  ");
        }
      }
    }