I want to sort my list of objects. I want to compare the Attribute elements by their names and compare the integer variable number of each object.
Below there is my code. I commented the line which doesn't work. How can I sort by the variable name of the non-primitive attribute Attribute of my Object?
List<Object> objects
List<Object> sortedObjects = objects.stream()
.sorted(Comparator.comparing(Object::getAtt().getName)) //this doesn't work.
.sorted(Comparator.comparing(Object::getNumber))
.collect(Collectors.toList());
public class Object {
Attribute att;
int number;
public Attribute getAtt() {
return att;
}
public int getNumber() {
return number;
}
}
public class Attribute {
String name;
public String getName() {
return name;
}
}
::denotes a method reference. You can only place a single name after it, not code. As rzwitserloot said, use a lambda if you want code.