java Comparator sorting by multiple fields

可紊 提交于 2021-02-16 15:45:50

问题


Need to sort class Dir by 3 String fields. First one: sort by 1 value -s1; Then sort by String length - s1+s2+s3; After sort by 2 value - s2; And sort by 3 value - s3. In my code its works for value 1 and String length sorting, but sorting by 2 value does not works. How i can sorting class Dir by multiple values? Class 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());
        }
    }
}

What is the solution? Tnx!


回答1:


Something like this:

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


来源:https://stackoverflow.com/questions/51755425/java-comparator-sorting-by-multiple-fields

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!