Home > Back-end >  Sort a List of object with 3 diferent numbers
Sort a List of object with 3 diferent numbers

Time:12-18

I have a list of objects

List<Values> mList = new Arraylist...

where a, b and c have different values which I don't know. I need to return the position of largest value of a and b and the smallest value of c. How to organize this list so that position 0 has the corresponding entry?

I tried something like

mList.sort(p1, p2 -> Math.max(p1.getValueA, Math.min(p1.getValueB, p2.getValueC))`

with no results...

(The values are int)

CodePudding user response:

It looks like you want to sort in descending order by the first property, then in descending order by the second property, then in ascending order by the third property. You can do so by chaining Comparators.

list.sort (Comparator.comparing (Values::getValueA).reversed ()
                     .thenComparing (Comparator.comparing (Values::getValueB).reversed ())
                     .thenComparing(Values::getValueC));
  • Related