0

I am trying to sort a nested VO with comparator. I am able to do it with collection sort, but intellij shows a warning we must use comparators. But I could not figure this usage when we have list of object with nest objects.

Reports Comparators defined as lambda expressions which could be expressed using methods like Comparator.comparing(). Some comparators like (person1, person2) -> person1.getName().compareTo(person2.getName()) could be simplified like this: Comparator.comparing(Person::getName). Also suggests to replace chain comparisons with Comparator.thenComparing(), e.g. int res = o1.first.compareTo(o2.first); if(res == 0) res = o1.second.compareTo(o2.second); if(res == 0) res = o1.third - o2.third; return res; will be replaced with objs.sort(Comparator.comparing((Obj o) -> o.first).thenComparing(o -> o.second).thenComparingInt(o -> o.third));

Here is my class

public class SortBaseVO {

public SortBaseVO(String prop1, String prop2, String prop3, String prop4, String sortBaseVO2) {
    this.prop1 = prop1;
    this.prop2 = prop2;
    this.prop3 = prop3;
    this.prop4 = prop4;
    this.sortBaseVO2 = sortBaseVO2;
}

private String prop1;

private String prop2;

private String prop3;

private String prop4;

private String sortBaseVO2;

public String getSortBaseVO2() {
    return sortBaseVO2;
}

public void setSortBaseVO2(String sortBaseVO2) {
    this.sortBaseVO2 = sortBaseVO2;
}

public String getProp1() {
    return prop1;
}

public void setProp1(String prop1) {
    this.prop1 = prop1;
}

public String getProp2() {
    return prop2;
}

public void setProp2(String prop2) {
    this.prop2 = prop2;
}

public String getProp3() {
    return prop3;
}

public void setProp3(String prop3) {
    this.prop3 = prop3;
}

public String getProp4() {
    return prop4;
}

public void setProp4(String prop4) {
    this.prop4 = prop4;
}

}

Class B

public class SortBaseVO2 {


    private String a;

    private String b;

    private String c;

    private String d;

    public String getA() {
        return a;
    }

    public void setA(String a) {
        this.a = a;
    }

    public String getB() {
        return b;
    }

    public void setB(String b) {
        this.b = b;
    }

    public String getC() {
        return c;
    }

    public void setC(String c) {
        this.c = c;
    }

    public String getD() {
        return d;
    }

    public void setD(String d) {
        this.d = d;
    }

}

Current sorting done is sortBaseVOList is a list of sortBaseVO

Collections.sort(sortBaseVOList , (sortBaseVO1, sortBaseVO2) ->
        sortBaseVO1.getsortBaseVO2().getB().concat(sortBaseVO1.getsortBaseVO2().getD())
        .compareTo(sortBaseVO2.getsortBaseVO2().getB().concat(sortBaseVO2.getsortBaseVO2().getD()));

Can I change this to use Comparator?

Ravindra Ranwala
  • 20,744
  • 6
  • 45
  • 63
Harry
  • 445
  • 3
  • 11
  • 28
  • Not sure if I understood the question right. If you don't want to define a comparator in the Collections.sort(), you have to implement Comparable interface by your VO classes. – androberz Jul 26 '19 at 10:59
  • Some types are wrong define in your example. For example, property SortBaseVO#sortBaseVO2 is define as String, but I guest that the type should be SortBaseVO2. – João Pedro Schmitt Jul 26 '19 at 11:03

1 Answers1

0

I simulated your problem.

I suggest the following alternative as the right way to fix this warning:

List<SortBaseVO> sortBaseVOList = new ArrayList<>();
Collections.sort(sortBaseVOList , Comparator.comparing(SortBaseVO::getConcat));

where in your SortBaseVO class you must to define the following method:

public class SortBaseVO {

    // all other methods

    public String getConcat() {
        return getSortBaseVO2().getB().concat(getSortBaseVO2().getD());
    }
}

João Pedro Schmitt
  • 1,046
  • 1
  • 11
  • 25