I have a list of my own Java objects, which need to be sorted by the property code of the object itself.
List<Calle> listaDeCalles;
Calle calle1 = new Calle();
calle1.setCodigo("A4");
...
listaDeCalles.add(calle1);
listaDeCalles.add(calle2);
listaDeCalles.add(calle3);
Is there any way to do it?
I know there is an option to do it with the Collections.sort
:
java.util.Collections.sort(listaDeCalles)
And do it with a Set:
Set<Calle> setDeCalles = new Set<Calle>();
But neither is viable because some methods like the equals
and the compareTo
have been previously overwritten. And also the idea is that it does not touch FOR ANYTHING the Street class or its parent class.
What would be ideal for this case would be that there exists a method which orders a list by the code property which is String
ascending or descending.