1


I have a Java ArrayList containing some objects of type ObjType.

Let's say the object has two fields:
- A boolean field isManual()
- A double value getAffinity()

I'm trying to order this ArrayList based on more than one criteria:
-First all the objects with isManual=true on the same order that they already have in the ArrayList
-Then I want all the objects with isManual=false ordered by the getAffinityValue (from the lowest to the greatest)

I've come up with this code, which is not working (it seems it's randomly sorting):

Collections.sort(coda, new Comparator<ObjType>() {

            public int compare(ObjType a, ObjType b) {
                boolean b1=a.isManual();
                boolean b2=b.isManual();
                if(b1 && b2) {
                    if (a.getAffinity() < b.getAffinity()) return 1;
                    if (a.getAffinity() > b.getAffinity()) return -1;
                    return 0;
                }
                if (b1) return -1;
                if (b2) return 1;
                if (a.getAffinity() < b.getAffinity()) return 1;
                if (a.getAffinity() > b.getAffinity()) return -1;
                return 0;
            }
}

Any suggestions? Thanks a lot!

8
  • 1
    Possible duplicate of How to sort by two fields in Java? Commented May 6, 2019 at 12:11
  • Hi @ThomasSallaberger. I tried that solution too and came up with some similar code to the one I currently have (just a little longer). That was not working for me Commented May 6, 2019 at 12:13
  • Sort twice. In the first sort, reorder the objects by isManual. In the second sort, ignore the objects where isManual is true and begin sorting where isManual is false. Commented May 6, 2019 at 12:14
  • Are you trying to display them in a JTable or similar? Commented May 6, 2019 at 12:15
  • sorry, seems like I misunderstood your question Commented May 6, 2019 at 12:15

1 Answer 1

1

First all the objects with isManual=true on the same order that they already have in the ArrayList

Then in the case where both objects have isManual == true you should not return anything other than 0.

Then I want all the objects with isManual=false ordered by the getAffinityValue (from the lowest to the greatest)

Then these lines:

if (a.getAffinity() < b.getAffinity()) return 1;
if (a.getAffinity() > b.getAffinity()) return -1; 

should return the opposite values that they do return.
So try this:

Collections.sort(coda, new Comparator<ObjType>() {
    public int compare(ObjType a, ObjType b) {
        boolean b1=a.isManual();
        boolean b2=b.isManual();

        if(b1 && b2) return 0;
        if (b1) return -1;
        if (b2) return 1;

        Double affinity1 = a.getAffinity();
        Double affinity2 = b.getAffinity();
        return affinity1.compareTo(affinity2);
    }
});
Sign up to request clarification or add additional context in comments.

1 Comment

Or as a one-liner: coda.sort((a, b) -> a.isManual()? b.isManual()? 0: -1: b.isManual()? +1: Double.compare(a.getAffinity(), b.getAffinity()));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.