-2

I have an array list with a object fields 4. I need to know how to order the fields of my object "one by one" using generic methods that perform algorithms (isort, msort, qsort).

I know the comparator interface but do not know how to check all fields in a generic type T.

Termininja
  • 6,620
  • 12
  • 48
  • 49
jhashing
  • 1
  • 1

1 Answers1

0

If I understood you right, you got a class that serves as a generic type and want to know how to use the Comparator interface with that. Here is my try:

class ClassWithFields {

    int fieldA;
    double fieldB;
    String fieldC;

    public ClassWithFields(int fieldA, double fieldB, String fieldC) {
        this.fieldA = fieldA;
        this.fieldB = fieldB;
        this.fieldC = fieldC;
    }

    @Override
    public String toString() {
        return "["+fieldA+","+fieldB+","+fieldC+"]";
    }

}

public class GenericComparator<T extends ClassWithFields> implements Comparator<T> {

    @Override
    public int compare(T o1, T o2) {
        if (o1.fieldA < o2.fieldA)
            return -1;
        else if (o1.fieldA > o2.fieldA)
            return +1;
        else if (o1.fieldB < o2.fieldB)
            return -1;
        else if (o1.fieldB > o2.fieldB)
            return +1;
        else
            return o1.fieldC.compareTo(o2.fieldC);
    }

    public static void main(String[] args) {
        ClassWithFields[] cwfArray = new ClassWithFields[3];
        cwfArray[0] = new ClassWithFields(2, 1.5, "Test");
        cwfArray[1] = new ClassWithFields(1, 3.5, "Test");
        cwfArray[2] = new ClassWithFields(2, 1.5, "Tast");
        Arrays.sort(cwfArray, new GenericComparator<ClassWithFields>());
        System.out.println(Arrays.toString(cwfArray));
    }

}

When you run the main method, the first array item will be put at the back of the array because 1 < 2 and 'a' < 'e' (also 3.5 < 1.5 but we don't go there because we have an order in which the fields are compared) Does that answer the question?

EDIT: This should do it now.

public class GenericComparator<T extends ClassWithFields> implements Comparator<T> {

    private String fieldIdentifier;

    public GenericComparator(String fieldIdentifier)
    {
        this.fieldIdentifier = fieldIdentifier;
    }

    @Override
    public int compare(T o1, T o2) {
        if (fieldIdentifier.equals("fieldA")) {
            if (o1.fieldA < o2.fieldA)
                return -1;
            else if (o1.fieldA > o2.fieldA)
                return +1;
            return 0;
        }
        else if (fieldIdentifier.equals("fieldB")) {
            if (o1.fieldB < o2.fieldB)
                return -1;
            else if (o1.fieldB > o2.fieldB)
                return +1;
            return 0;
        }
        else
            return o1.fieldC.compareTo(o2.fieldC);
    }

    public static <S extends ClassWithFields> void isort(ArrayList<S> array, String type) {
        Comparator<S> comp = new GenericComparator<S>(type);
        // TODO employ search algorithm
        S help = null;
        for (int i = 0; i < array.size(); i++) {
            for (int j = 0; j < array.size(); j++) {
                if (comp.compare(array.get(i), array.get(j)) > 0) {
                    help = array.get(i);
                    array.set(i, array.get(j));
                    array.set(j, help);
                }
            }
        }
    }

    public static void main(String[] args) {
        ClassWithFields[] cwfArray = new ClassWithFields[3];
        cwfArray[0] = new ClassWithFields(2, 1.5, "Test");
        cwfArray[1] = new ClassWithFields(1, 3.5, "Test");
        cwfArray[2] = new ClassWithFields(2, 1.5, "Tast");
        ArrayList<ClassWithFields> cwfList = new ArrayList<ClassWithFields>();
        Collections.addAll(cwfList, cwfArray);
        isort(cwfList, "fieldA");
        System.out.println(Arrays.toString(cwfList.toArray()));
    }

}
GreenThor
  • 456
  • 5
  • 14
  • it is as you have described, the only thing is that I can not use array.sort, but a sorting method with an array parameter list and a string that tells me how I have to sort the array list (depending on which field of 'ClassWithFields object). this should be the method header. public static void isort(ArrayList array,String type) – jhashing Jun 10 '16 at 15:11
  • I realized by reading the specifications that can not extend or specify classes where I implement the sort methods; why they should remain generic and can work with any data class – jhashing Jun 10 '16 at 17:28