0

I'm trying to sort a list of objects, by a property of that object. For example a list called "persons", object called "person" and some properties like "age", "length" and "motherlanguage". I want to sort this list by either age, length or motherlanguage. Normally you would do this as followed:

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());

Problem is, the list of properties is quite long so making a comparator for each property in 'Android' java is too much work (it's doable, but i'm lazy), so I want to a comparator with reflection. In java this is called a bean comparator but i don't know how i can implement something like this in 'Android' java.

Malone
  • 71
  • 1
  • 5

2 Answers2

1

The BeanComparator Util is part of the Apache Commons BeanUtil.

This post https://stackoverflow.com/a/23408171/847592 mentions about accessing these libraries via importing the android-java-air-bridge.jar

Community
  • 1
  • 1
Himanshu Soni
  • 264
  • 8
  • 15
0

Since the guy in that post said that the solution "wasn't dandy" i've created my own bean comparator class, which includes the sort direction. here below is my solution:

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;
}

}

Malone
  • 71
  • 1
  • 5