-
Notifications
You must be signed in to change notification settings - Fork 66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Nazarov_Anton/part-1 #162
base: master
Are you sure you want to change the base?
Nazarov_Anton/part-1 #162
Conversation
Arrays.sort(persons, new Comparator<Person>() { | ||
@Override | ||
public int compare(Person o1, Person o2) { | ||
return o2.getFirstName().compareTo(o1.getFirstName()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See method name: sortPersonsByAge
.
@@ -18,7 +22,7 @@ public void sortPersonsByAge() { | |||
new Person("name 2", "lastName 1", 30) | |||
}; | |||
|
|||
// TODO use Arrays.sort | |||
Arrays.sort(persons, (o1, o2) -> Integer.valueOf(o1.getAge()).compareTo(o2.getAge())); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use Comparator.comparingInt
to avoid boxing.
@@ -27,7 +27,7 @@ public void sortPersonsByAge() { | |||
Arrays.sort(persons, new Comparator<Person>() { | |||
@Override | |||
public int compare(Person o1, Person o2) { | |||
return o2.getFirstName().compareTo(o1.getFirstName()); | |||
return Comparator.<Person>comparingInt(Person::getAge).compare(o1, o2); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are creating a new Comparator
on each compare
here.
Use Integer.compare
.
@@ -22,7 +24,7 @@ public void sortPersonsByAge() { | |||
new Person("name 2", "lastName 1", 30) | |||
}; | |||
|
|||
Arrays.sort(persons, (o1, o2) -> Integer.valueOf(o1.getAge()).compareTo(o2.getAge())); | |||
Arrays.sort(persons, (o1, o2) -> Comparator.comparingInt(Person::getAge).compare(o1, o2)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the type of Comparator.comparingInt(Person::getAge)
?
No description provided.