我想通過該對象的屬性對對象列表進行排序。例如名爲「人物」的名單,稱爲「人物」的物品以及諸如「年齡」,「長度」和「母語」等一些物品。我想按照年齡,身高或母語排列這份名單。通常你會做如下:Android中的Bean比較器
public class PersonComparator implements Comparator<Person> {
@Override
public int compare(Person person1, Person person2) {
return person1.getName().compareTo(person2.getName());
}
}
Collections.sort(persons, new PersonComparator());
問題是,屬性列表相當長,所以使得在對「Android的Java每個屬性比較是太多的工作(這是可行的,但我很懶),所以我想用反射比較一下。在Java中,這被稱爲豆比較器,但我不知道我怎麼能在'Android'java中實現這樣的東西。