JAVA基礎之物件的排序

Anstrue發表於2017-03-31

基本資料型別的排序

在JAVA語言中,對於基本的資料型別組成的陣列或者集合,都可以使用sort()方法進行排序。因為這些基本資料型別都實現了比較方法

public class test {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()){
            String[] stringArray = new String[5];
            for (int i = 0; i < 5; i++) {
                String s = sc.nextLine();
                stringArray[i] = s;
            }
            Arrays.sort(stringArray);
            for (int i = 0; i < 5; i++) {
                System.out.println(stringArray[i]);
            }
        }
    }
}

hello 
java 
i
like 
programming
hello 
i
java 
like 
programming
使用sort方法可以實現對基本資料型別的字典排序

物件排序

讓實體類繼承Comparable介面,重寫CompareTo方法

public static void main(String[] args) {
        ArrayList<student> list = new ArrayList<student>();
        list.add(new student(20,"zhangsan",80,80));
        list.add(new student(22,"lisi",75,90));
        list.add(new student(30,"wangwu",90,65));
        Collections.sort(list,new UserCompatator());
        for (student stu:list){
            System.out.println(stu.toString());
        }
    }

class student implements Comparable{
    int age;
    String name;
    int chinese;
    int math;

    public student(int age, String name, int chinese, int math) {
        this.age = age;
        this.name = name;
        this.chinese = chinese;
        this.math = math;
    }

    @Override
    public String toString() {
        return "student{" +
                "age=" + age +
                ", name='" + name + '\'' +
                ", chinese=" + chinese +
                ", math=" + math +
                '}';
    }

    @Override
    public int compareTo(Object o) {
        return  ((student) o).chinese - this.chinese;
    }
}

該方法實現了基於語文成績的排序


使用lambda表示式

Collections.sort(stus,(e1,e2)->{
            if(e1.chinese==e2.chinese){
                return Integer.compare(e1.math,e2.math);
            }else {
                return Integer.compare(e1.chinese,e2.chinese);
            }
        });

        for (student stu:stus){
            System.out.println(stu);
        }



構造介面Comparator介面的實現類,重寫Compare方法

public static void main(String[] args) {
        ArrayList<student> list = new ArrayList<student>();
        list.add(new student(20,"zhangsan",80,80));
        list.add(new student(22,"lisi",75,90));
        list.add(new student(30,"wangwu",90,65));
        //原始順序
        System.out.println("原始順序");
        for (student stu:list){
            System.out.println(stu.toString());
        }
        //按照年齡排序
        System.out.println("按照年齡排序");
        Collections.sort(list,new ageSort());
        for (student stu:list){
            System.out.println(stu.toString());
        }
        //按照姓名排序,字典序
        System.out.println("按照姓名排序");
        Collections.sort(list,new nameSort());
        for (student stu:list){
            System.out.println(stu.toString());
        }
        //按照語文成績排序
        System.out.println("按照語文成績排序");
        Collections.sort(list,new chineseSort());
        for (student stu:list){
            System.out.println(stu.toString());
        }
    }
}


class ageSort implements Comparator{   //簡單修改可實現逆序
    @Override
    public int compare(Object o1, Object o2) {
        student s1 = (student)o1;
        student s2 = (student)o2;
        return s1.chinese < s2.chinese ? 1 : (s1 == s2 ? 0 : -1);
    }
}

class nameSort implements Comparator{
    @Override
    public int compare(Object o1, Object o2) {
        student s1 = (student)o1;
        student s2 = (student)o2;
        return s1.name.compareTo(s2.name);
    }
}

class chineseSort implements Comparator{
    @Override
    public int compare(Object o1, Object o2) {
        student s1 = (student)o1;
        student s2 = (student)o2;
        return s1.chinese - s2.chinese;
    }
輸出:

原始順序
{age=20, name='zhangsan', chinese=80, math=80}
{age=22, name='lisi', chinese=75, math=90}
{age=30, name='wangwu', chinese=90, math=65}
按照年齡排序
{age=30, name='wangwu', chinese=90, math=65}
{age=20, name='zhangsan', chinese=80, math=80}
{age=22, name='lisi', chinese=75, math=90}
按照姓名排序
{age=22, name='lisi', chinese=75, math=90}
{age=30, name='wangwu', chinese=90, math=65}
{age=20, name='zhangsan', chinese=80, math=80}
按照語文成績排序
{age=22, name='lisi', chinese=75, math=90}
{age=20, name='zhangsan', chinese=80, math=80}
{age=30, name='wangwu', chinese=90, math=65}


二維陣列排序

Collections.sort(list, new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                return  o1[0]-o2[0];
            }
        });

Arrays.sort(str, new Comparator<String>(){
            @Override
            public int compare(String o1, String o2) {
                String s1 = o1 + o2;
                String s2 = o2 + o1;
                return s1.compareTo(s2);
            }

        });

相關文章