How to sort an abstract collection by the implementation classes?
<p>I have a <code>Collection&lt;BasePerson&gt;</code>, where BasePerson is an <code>abstract</code> class sharing common properties.</p>

<pre><code>abstract class BasePerson {
    String name;
}

class Person extends BasePerson {

}

class Employee extends BasePerson {
    int id;
}
</code></pre>

<p>How can I sort the collection by type? I want to have Any <code>Person</code> to be sorted by <code>name</code> and on top of any "non-person" classes.</p>

<p>The <code>Employee</code> objects themself should be sorted first by <code>id</code>, then by <code>name</code></p>

<p>Input:</p>

<pre><code>#name;id
xava (Person)
anna,31 (Employee)
tom,30 (Employee)
</code></pre>

<p>output:</p>

<pre><code>xava (persons before employees)
tom,30 (id should have a higher weight than name comparison)
anna,31
</code></pre>

<p>How can I create such a <code>Comparator&lt;Person&gt;</code>?</p>

<pre><code>new Comparator&lt;BasePerson&gt;() {
    @Override
    public int compare(BasePerson p1, BasePerson p2) {
        if (p1 instanceof Person) {
            if (p2 instanceof Person) {
                return p1.getName().compareTo(p2.getName());    
            }

            return 1;
        } else if (p2 instanceof Person) {
            return -1;
        }

        return 0;
    }
}
</code></pre>