java比较器按多个字段排序

时间:2018-08-08 20:48:53

标签: java sorting comparator

需要按3个String字段对Dir类进行排序。 第一个:按1个值排序- s1 ; 然后按字符串长度- s1+s2+s3 进行排序; 按2个值排序后- s2 ; 然后按3个值- s3 进行排序。 在我的代码中,它适用于值1和字符串长度排序,但按2值排序不起作用。 我如何按多个值对Dir类进行排序? 班长:

    public class Dir {

    private String s1 = "";
    private String s2 = "";
    private String s3 = "";

    public Dir(String s1){
        this.s1 = s1;
    }

    public Dir(String s1, String s2){
        this.s1 = s1;
        this.s2 = s2;
    }

    public Dir(String s1, String s2, String s3){
        this.s1 = s1;
        this.s2 = s2;
        this.s3 = s3;
    }

    public String getS1() {
        return s1;
    }

    public void setS1(String s1) {
        this.s1 = s1;
    }

    public String getS2() {
        return s2;
    }

    public void setS2(String s2) {
        this.s2 = s2;
    }

    public String getS3() {
        return s3;
    }

    public void setS3(String s3) {
        this.s3 = s3;
    }

    @Override
    public String toString() {
        return s1 + "\\" + s2 +"\\"+ s3 ;
    }
}





    //Comparator for 2 value:
    public class DirReversComparatorS2 implements Comparator<Dir> {
    @Override
    public int compare(Dir o1, Dir o2) {

        if ((o1.getS2().compareTo(o2.getS2())) > 0) {
            return -1;
        }
        else if ((o1.getS2().compareTo(o2.getS2())) < 0) {
            return 1;
        }
        else {
            return 0;
        }
    }
}

//Comparator for 3 value
public class DirReversComparatorS3 implements Comparator<Dir> {
@Override
public int compare(Dir o1, Dir o2) {


    if ((o1.getS3().compareTo(o2.getS3())) > 0) {
        return -1;
    } else if ((o1.getS3().compareTo(o2.getS3())) < 0) {
        return 1;
    }
    else {
        return 0;
    }
}

}

//Comparator for string lenght:

public class DirReversStringComparator implements Comparator<Dir> {
@Override
public int compare(Dir o1, Dir o2) {
    if (o1.toString().length() > o2.toString().length()) {
        return 1;
    }
    else {
        return -1;
    }

}

}

    //Main class:
    public class Main {
    public static void main(String[] args){

        Comparator<Dir> dcomp = new DirComparatorS1().thenComparing(new DirComparatorS2(
        ).thenComparing(new DirComparatorS3()));
        TreeSet<Dir> dir = new TreeSet<>(dcomp);
        dir.add(new Dir("K1"));
        dir.add(new Dir("K1","SK2"));
        dir.add(new Dir("K1","SK1","SSK2"));
        dir.add(new Dir("K1", "SK1", "SSK1"));
        dir.add(new Dir("K2"));
        dir.add(new Dir("K2", "SK2", "SSK1"));
        dir.add(new Dir("K2", "SK2", "SSK2"));
        dir.add(new Dir("K2", "SK3", "SSK2"));
        dir.add(new Dir("K2", "SK2", "SSK3"));



        System.out.println("Отсортирован по возрастанию");
        for(Dir d : dir){
            System.out.println(d.getS1()+"\\"+d.getS2()+"\\"+d.getS3());
        }


        //This code does not works!
        Comparator<Dir> reverse = new DirReversComparatorS1().thenComparing(
                new DirReversStringComparator()).thenComparing(
                        new DirReversComparatorS2()).thenComparing(
                                new DirReversComparatorS3());


        TreeSet<Dir> rdir = new TreeSet<>(reverse);
        rdir.add(new Dir("K1"));
        rdir.add(new Dir("K1","SK2"));
        rdir.add(new Dir("K2","SK2"));
        rdir.add(new Dir("K1","SK1","SSK2"));
        rdir.add(new Dir("K1", "SK1", "SSK1"));
        rdir.add(new Dir("K1", "SK1", "SSK2"));
        rdir.add(new Dir("K2"));
        rdir.add(new Dir("K2", "SK4", "SSK3"));
        rdir.add(new Dir("K2", "SK2", "SSK5"));
        rdir.add(new Dir("K2","SK1"));
        rdir.add(new Dir("K2", "SK2", "SSK1"));
        rdir.add(new Dir("K1", "SK1"));
        rdir.add(new Dir("K2", "SK2", "SSK2"));
        rdir.add(new Dir("K1", "SK2"));
        rdir.add(new Dir("K2", "SK3", "SSK2"));
        rdir.add(new Dir("K2", "SK2", "SSK3"));
        rdir.add(new Dir("K2", "SK2", "SSK5"));
        rdir.add(new Dir("K2", "SK1", "SSK3"));
        rdir.add(new Dir("K1", "SK3"));
        rdir.add(new Dir("K1", "SK3", "SSK1"));


        System.out.println("Отсортирован по убыванию");
        for(Dir d : rdir){
            System.out.println(d.getS1()+"\\"+d.getS2()+"\\"+d.getS3());
        }
    }
}

解决方案是什么? Tnx!

1 个答案:

答案 0 :(得分:3)

类似这样的东西:

dirs
   .sort(Comparator.comparing(Dir::getS1)
                    .thenComparingInt(
                            d -> d.getS1().length() + d.getS2().length() + d.getS3().length())
                    .thenComparing(Dir::getS2)
                    .thenComparing(Dir::getS3));