import
java.util.ArrayList;
import
java.util.Collections;
import
java.util.Comparator;
import
java.util.List;
public
class
Person {
private
String name;
private
int
age;
private
String city;
public
Person(String name,
int
age, String city)
{
this
.name = name;
this
.age = age;
this
.city = city;
}
public
String getName() {
return
name; }
public
int
getAge() {
return
age; }
public
String getCity() {
return
city; }
public
static
class
PersonComparator
implements
Comparator<Person> {
@Override
public
int
compare(Person p1, Person p2)
{
int
nameComparison
= p1.getName().compareTo(p2.getName());
if
(nameComparison !=
0
) {
return
nameComparison;
}
int
ageComparison
= Integer.compare(p1.getAge(), p2.getAge());
if
(ageComparison !=
0
) {
return
ageComparison;
}
return
p1.getCity().compareTo(p2.getCity());
}
}
public
static
void
main(String[] args)
{
List<Person> people =
new
ArrayList<>();
people.add(
new
Person(
"John"
,
25
,
"New York"
));
people.add(
new
Person(
"Alice"
,
30
,
"London"
));
people.add(
new
Person(
"Bob"
,
25
,
"Paris"
));
Collections.sort(people,
new
PersonComparator());
for
(Person person : people) {
System.out.println(person.getName() +
", "
+ person.getAge() +
", "
+ person.getCity());
}
}
}