class PersonsApp {
	public static void main(String[] args) {
		Person[] persons = {
			new Person(2001, 25, new Name("Weiss", "Gerald")),
			new Person(1000, 30, new Name("Arnow", "David")),
			new Person(1500,  20, new Name("Sokol", "Dina"))
		};

		System.out.println("Sorted by age:");
		sort(persons, new Person.ByAge());
		System.out.println(toString(persons));

		System.out.println();

		System.out.println("Sorted by name:");
		sort(persons, new Person.ByName());
		System.out.println(toString(persons));
	}

	static void sort(Person [] arr, Person.Comparator comparator) {
		for (int last = arr.length-1; last > 0; last--) 
			for (int i = 0; i < last; i++)
				if (comparator.compare(arr[i], arr[i+1]) > 0) 
					swap(arr, i, i+1);
	}

	static void swap(Person [] arr, int i, int j) {
		Person t = arr[i];
		arr[i] = arr[j];
		arr[j] = t;
	}

	static String toString(Person [] arr) {
		String result = "{";
		for (int i = 0; i < arr.length; i++)
			result += arr[i] + (i < arr.length-1 ? ", " : "");
		return result + "}";
	}
}	
