Sort Object by two attributes
<p>Let's say I have a List with Students: <code>ArrayList&lt;Student&gt; student = new ArrayList&lt;&gt;();</code>. The Student-class looks like this:</p>

<pre><code>public class Student {
    String name;
    int age;
    String hobby;

    //getter and Setter methods below
}
</code></pre>

<p>I want to sort that list by name, but if the names are equal, I want to sort it after the age. How can I do this in Java.</p>

<p>With this I can already sort it by name:</p>

<pre><code>public class MyComparator implements Comparator&lt;Student&gt;{

    @Override
    public int compare(Student o1, Student o2) {
        if(o1.getName() &gt; o2.getName()){
            return 1;
        }
        else if(o1.getName() &lt; o2.getName())
        {
            return -1;
        }
        else{
            return 0;
        }
    }
}
</code></pre>

<p>So if they have the same name, how can I sort it after the age?
In the end I want to do <code>Collections.sort(student, MyComparator comp)</code> to sort the list.</p>