2016-03-02 68 views
0

我想通過該對象的屬性對對象列表進行排序。例如名爲「人物」的名單,稱爲「人物」的物品以及諸如「年齡」,「長度」和「母語」等一些物品。我想按照年齡,身高或母語排列這份名單。通常你會做如下: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中實現這樣的東西。

回答

1

BeanComparator Util是Apache Commons BeanUtil的一部分。

這篇文章https://stackoverflow.com/a/23408171/847592提到關於由於該職位的傢伙說,解決「不是花花公子」我已經創建了自己的豆腐比較類,其中包括排序通過導入android-java-air-bridge.jar

0

訪問這些庫方向。這裏下面是我的解決方案:

package denavo.ape; 

import java.lang.reflect.Method; 
import java.util.Comparator; 

/** 
* Created by Dennis Van de Vorst on 3-3-2016. 
*/ 

public class _ApeItemComparator implements Comparator<_ApeItem> { 




////////////////////////////////////////////////////////////////////////////////////////////// 
///////////////////////////////////////// Variables ////////////////////////////////////////// 
////////////////////////////////////////////////////////////////////////////////////////////// 

Method method; 
Boolean invertDirection; 




////////////////////////////////////////////////////////////////////////////////////////////// 
///////////////////////////////////////// functions ////////////////////////////////////////// 
////////////////////////////////////////////////////////////////////////////////////////////// 

////////////////////////////////////////////////////////////////////////////////////////////// 
// constructor 

_ApeItemComparator(String methodName, Boolean invertDirection) { 

    // 1. set invertDirection 
    this.invertDirection = invertDirection; 

    // 2. set method 
    try { 
     this.method = _ApeItem.class.getMethod(methodName, null); 
    } catch(Exception e){ 
     e.printStackTrace(); 
     this.method = null; 
    } 

} 



////////////////////////////////////////////////////////////////////////////////////////////// 
// functions : compare 

@Override 
public int compare(_ApeItem apeItem1, _ApeItem apeItem2) { 

    // 1. declare variables 
    String property1 = null, property2 = null; 

    // 2. get the property strings 
    try{ 
     property1 = (String) method.invoke(apeItem1, null); 
     property2 = (String) method.invoke(apeItem2, null); 
    } catch(Exception e){ 
     e.printStackTrace(); 
    } 

    // 3. if the direction is inverted, swap the two strings 
    if (invertDirection){ 
     String temp = property1; 
     property1 = property2; 
     property2 = temp; 
    } 

    // 4. check if the strings are doubles 
    if(property1.matches("([0-9]*)\\.([0-9]*)") && property2.matches("([0-9]*)\\.([0-9]*)")) 
     return compareStringsAsDoubles(property1, property2); 

    // 5. check if they are integers 
    else if(property1.matches("[0-9]+") && property2.matches("[0-9]+")) 
     return Integer.parseInt(property1)-Integer.parseInt(property2); 

    // 6. if they are strings then compare them as strings 
    else 
     return property1.compareTo(property2); 
} 



////////////////////////////////////////////////////////////////////////////////////////////// 
// functions : compare strings as doubles 

private int compareStringsAsDoubles(String double1, String double2){ 
    if (Double.parseDouble(double1) < Double.parseDouble(double2)) 
     return -1; 
    else if (Double.parseDouble(double1) == Double.parseDouble(double2)) 
     return 0; 
    else 
     return 1; 
} 

}