简体   繁体   中英

Java best way to sort a list by another list with different objects

Lets say I have a list with objects, lets call it listA which is unordered, and it has objects of type A . type A has a field called name . now, I have another list, which is ordered. let's call it listB and it is ordered. the objects in this list are of type B and they also had a field name .

Now, I want listA to be ordered as listB , and the only thing that is the same in those two lists is the field name which is the same in both objects.

what is the most efficient way to do so?

I hope I'm understanding your problem.

Is the field 'name' a common field shared by the two types of Object (A and B)? In other words, do the two types (A and B) extend the same Class C that contains the field 'name'? If so you could implement the Comparable in Class C and then Collections.sort() would do the trick for you. All you have to do, while implementing the comparable, would be to 'teach' it how to compare the field 'name' so that it orders, in the same manner, the list of type B objects, you specify, are ordered.

Another way to do this , in case your classes don't have anything in common, therefore not making sense of having a superclass C, would be to create a Comparator interface, where you could specify the fields that can be used to compare two distinct objects and then do the sorting using those fields, in other words:

  1. Create a Interface with the method getCompareFields() returns String[]; (for example)
  2. Implement interface of point 1. in objects A and B
  3. Implement a sorting algorithm that uses this interface's getCompareFields() method for sorting.

And, another way even, for you to do this, but, in my opinion, would be more complicated, would be to use reflection to get the field 'name' of both objects, in case they are not related in any kind.

I hope this helps you.

EDIT Example of reflection:

Class TypeA (Example)

public class TypeA {
private String name;
private String id;

public TypeA() {
    this(null, null);
}

public TypeA(String name, String id) {
    this.name = name;
    this.id = id;
}

public String getName() {
    return this.name;
}

public String getId() {
    return this.id;
}

@Override
public String toString() {
    return "(" + name + ", " + id + ")";
}

}

Class TypeB (Example)

public class TypeB {
private String name;
private List<Integer> numbers;

public TypeB() {
    this(null);
}

public TypeB(String name, int ... numbers) {
    this.name = name;

    this.numbers = new LinkedList<Integer>();
    if (numbers != null) {
        for (int i : numbers) {
            this.numbers.add(i);
        }
    }
}

public String getName() {
    return this.name;
}

public List<Integer> getANumber() {
    return this.numbers;
}

@Override
public String toString() {
    StringBuilder builder = new StringBuilder();
    builder.append("(").append(name).append(", [");

    Iterator<Integer> i = this.numbers.iterator();

    if (i.hasNext()) {
        builder.append(i.next());

        while (i.hasNext()) {
            builder.append(", ").append(i.next());
        }
    }

    builder.append("])");
    return builder.toString();
}

}

Reflection Name Comparator:

public class NameReflectionComparator implements Comparator<Object> {

@Override
public int compare(Object o1, Object o2) {
    if (o1 != null && o2 != null) {
        try {
            Method obj1_getNameMethod = o1.getClass().getMethod("getName");
            Method obj2_getNameMethod = o2.getClass().getMethod("getName");

            String obj1_name = (String) obj1_getNameMethod.invoke(o1);
            String obj2_name = (String) obj2_getNameMethod.invoke(o2);

            if (obj1_name != null) {
                return obj1_name.compareTo(obj2_name);
            } else if (obj2_name != null) {
                return obj2_name.compareTo(obj1_name);
            }
        } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
            e.printStackTrace();
        }

    } else if (o1 != null && o2 == null) {
        return 1;
    } else if (o1 == null && o2 != null) {
        return -1;
    }

    return 0;
}

}

Main:

public class Main {
public static void main(String[] args) {

    // both TypeA and TypeB have field name and method getName()
    // unsorted list
    List<TypeA> typeAList = new LinkedList<TypeA>();
    typeAList.add(new TypeA("Yves Larock", UUID.randomUUID().toString()));
    typeAList.add(new TypeA("I'm ok", UUID.randomUUID().toString()));
    typeAList.add(new TypeA("Aaah", UUID.randomUUID().toString()));
    typeAList.add(new TypeA("Noooh", UUID.randomUUID().toString()));

    // sorted list
    List<TypeB> typeBList = new LinkedList<TypeB>();
    typeBList.add(new TypeB("Aaah", 1, 2, 3));
    typeBList.add(new TypeB("I'm ok", 1));
    typeBList.add(new TypeB("Noooh", 34, 3));
    typeBList.add(new TypeB("Yves Larock", 4, 5, 3, 9));

    System.out.println("ListA:\n" + typeAList);
    System.out.println("ListB:\n" + typeBList);

    NameReflectionComparator comparator = new NameReflectionComparator();
    Collections.sort(typeAList, comparator);


    System.out.println("=== AFTER SORT ====\nListA:\n" + typeAList);
    System.out.println("ListB:\n" + typeBList);
}

}

Result:

ListA: [(Yves Larock, f40cb523-58e8-4ee2-aa4f-991ce7e7cdd5), (I'm ok, 5a66b9d9-7a27-4529-ab64-c893291bd9b0), (Aaah, 4842fd55-47e5-48ac-b7b6-ebc7acf7023c), (Noooh, 8dc89675-bc28-4374-aff2-0c5d0ae6dd9d)]

ListB: [(Aaah, [1, 2, 3]), (I'm ok, 1 ), (Noooh, [34, 3]), (Yves Larock, [4, 5, 3, 9])]

=== AFTER SORT ==== ListA: [(Aaah, 4842fd55-47e5-48ac-b7b6-ebc7acf7023c), (I'm ok, 5a66b9d9-7a27-4529-ab64-c893291bd9b0), (Noooh, 8dc89675-bc28-4374-aff2-0c5d0ae6dd9d), (Yves Larock, f40cb523-58e8-4ee2-aa4f-991ce7e7cdd5)]

ListB: [(Aaah, [1, 2, 3]), (I'm ok, 1 ), (Noooh, [34, 3]), (Yves Larock, [4, 5, 3, 9])]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM