package Testate.Stream.SortAlgorithm;

import java.util.Arrays;

public class Person implements Comparable<Person> {
	
	String name;
	int alter;

	public Person(String name, int alter) {
		super();
		this.name = name;
		this.alter = alter;
	}

	@Override
	public String toString() {
		return "Person [name=" + name + ", alter=" + alter + "]";
	}

	@Override
	public int compareTo(Person p) {
		if (this.alter > p.alter) // Aufsteigend
			return 1;
		else
			return -1;
	}


	public static void main(String[] args) {
		Person[] personen = {new Person("obai",14),new Person("Ahmad",20), new Person("Abd",12)};
		bubbleSort(personen);
		for (Person p: personen)
			System.out.println(p.toString());
	}
	
	public static void bubbleSort(Person[] p) {
		boolean tauche;
		for (int i = 0; i < p.length - 1; i++) {
			tauche = false;
			for (int j = 0; j < p.length-1; j++)
				if (p[j].compareTo(p[j + 1]) > 0) { // >0: Aufsteigend sortieren
					Person temp = p[j];
					p[j] = p[j + 1];
					p[j + 1] = temp;
					tauche = true;
				}
			if (!tauche)
				break;
		}
	}

}
