import java.util.Collections;
import java.util.ArrayList;
class Person implements Comparable<Person>{
protected String name;
protected int age;
protected Person (String name, int age) {
this.name=name;
this.age=age;
}
public String getName() {
return name;
}
@Override
public int compareTo(Person p){
/*
* Sorting by last name. compareTo should return < 0 if this(keyword)
* is supposed to be less than p, > 0 if this is supposed to be
* greater than object p and 0 if they are supposed to be equal.
*/
int result = name.compareTo(p.name);
if(result!=0)
return result;
result=age-p.age;
return result;
}
@Override
public String toString() {
return "["+name+","+age+"]";
}
}
public class Main
{
public static void main(String[] args) {
ArrayList<Person> group=new ArrayList<>();
group.add(new Person("Вася",30));
group.add(new Person("Соня",23));
group.add(new Person("Женя",28));
group.add(new Person("Вася",28));
Collections.sort(group);
for(Person p: group)
System.out.println(p);
}
}