如果我有一个简单的字符串列表:
List<String> stringList = new ArrayList<String>();
Run Code Online (Sandbox Code Playgroud)
我可以用它来分类:
Collections.sort(stringList);
Run Code Online (Sandbox Code Playgroud)
但是假设我有一个Person类:
public class Person
{
private String name;
private Integer age;
private String country;
}
Run Code Online (Sandbox Code Playgroud)
并列出一个:
List<Person> personList = new ArrayList<Person>();
Run Code Online (Sandbox Code Playgroud)
我希望有时按名字排序,有时按年龄,有时按国家排序.
最简单的方法是什么?
我知道我可以实现Comparable接口,但这似乎限制我按一个特定属性对其进行排序.