Skip to content
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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Nazarov_Anton/part-1 #162

wants to merge 3 commits into from

Conversation

ghost
Copy link

@ghost ghost commented Jul 4, 2017

No description provided.

Arrays.sort(persons, new Comparator<Person>() {
@Override
public int compare(Person o1, Person o2) {
return o2.getFirstName().compareTo(o1.getFirstName());
Copy link
Contributor

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()));
Copy link
Contributor

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.

@senia-psm senia-psm mentioned this pull request Jul 4, 2017
@@ -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);
Copy link
Contributor

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));
Copy link
Contributor

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)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant