Say I have an Object composed of a String and an Integer. How would I make an ArrayList<Object> with the objects with the highest integer value first and lowest last?
Asked
Active
Viewed 39 times
-3
Mureinik
- 277,661
- 50
- 283
- 320
Robert Colburn
- 29
- 7
-
5have you tried anything? – Reimeus Sep 30 '18 at 21:12
-
Search Stack Overflow thoroughly before posting. – Basil Bourque Sep 30 '18 at 21:26
1 Answers
1
You could sort them with a customer Comparator based on that property. With Java 8's enhancements, it should be pretty elegant:
myList.sort(Comparator.comparingInt(MyObject::getIntegerProperty).reversed());
Mureinik
- 277,661
- 50
- 283
- 320