Sort two different fields in one comparator
<blockquote>
  <p><strong>Possible Duplicate:</strong><br>
  <a href="http://stackoverflow.com/questions/369512/best-way-to-compare-objects-by-multiple-fields">Best way to compare objects by multiple fields?</a>  </p>
</blockquote>



<p>Hello i'm kinda new to programming and would like to ask help, my teacher created a class wherein the country, continent, and population is given. The desired output is to get the top 10 populated countries for each continent.</p>

<p>Code:</p>

<pre><code>import java.util.*;

public class WP {

public static void main(String[] args) {
    WorldPop[] list = WorldPop.getList();
    int index;

    Arrays.sort(list, new PopComparator());

    for (int i=0; i&lt;list.length; i++) {
        System.out.printf("%-53s %-20s %,14d\n", list[i].country, 
                       list[i].continent, list[i].population);
    }
}
}

class PopComparator implements Comparator&lt;WorldPop&gt; {
public int compare(WorldPop x, WorldPop y) {
    return x.population - y.population;
}
}

class TitleComparator implements Comparator&lt;WorldPop&gt; {
public int compare(WorldPop x, WorldPop y){
    return x.continent.compareTo(y.continent);
}
}
</code></pre>
