How to sort by a field of class with its own comparator?
问题 I have a sample class, say Country : class Country { public String name; public int population; public Flag flag; ... } I have this Flag class defined somewhere else class Flag { String id; int pixel; ... } Now I create a separate comparator DefaultFlagRankingComparator() that can sort Flag by id. How can I sort a list of Country by Flag id, using this DefaultFlagRankingComparator()? 回答1: You can invoke the compare method of the Comparator with the flag field of each country.