I am having an Arraylist of Objects. Those object have an attribute or datatype - 'String'. I need to sort the Arraylist by that string. How to achieve this?
You need to write a Or else, your See alsoRelated questionsOn sorting On | |||||||||||||||||
|
Another good way of doing this that is a bit more flexible if there is more than one property of an object that you may wish to sort by is to use Guava's Ordering class with its
The above will sort the list by first name. To make it read nicer, you may want to define the functions you might want to use as
As a fun aside note, this should all be a lot easier in Java 7 with lambda expressions and method references. As the plan stands now, you'll probably be able to write something like this without having to define any clumsy anonymous inner classes or static final fields:
There could even be something like this:
| ||||
|