Java sort arraylist of objects by the field of object

www.‮.iruttual‬com
Java sort arraylist of objects by the field of object
/**
 * @author lautturi.com 
 * Java example: sort a list of objects according to name in java
 */

import java.util.*;
class Person{
	String name;
	int age;
	
	public Person(String string, int i) {
		name = string;
		age = i;
	}

	public String getName() {
		return name;
	}
	public int getAge() {
		return age;
	}
}
public class Lautturi {

	public static void main(String[] args) {
		List<Person> persons = new ArrayList<Person>();
		persons.add(new Person("Alan",13));
		persons.add(new Person("Bob",25));
		persons.add(new Person("James",22));
		persons.add(new Person("Tom",22));
		persons.add(new Person("Sally",23));
		persons.add(new Person("Leo",29));
		persons.add(new Person("Haley",27));
		persons.add(new Person("Martin",18));

		Collections.sort(persons, (p1,p2) ->  p1.getName().compareTo(p2.getName()));
		
		for (Person p : persons) {
			System.out.println(p.getName()+" "+p.getAge());
		}
	}
}

output:

Alan 13
Bob 25
Haley 27
James 22
Leo 29
Martin 18
Sally 23
Tom 22
Created Time:2017-10-09 13:12:22  Author:lautturi