简体   繁体   English

按另一个数组对对象的数组列表进行排序

[英]Sort Arraylist of Objects by another array

Objects in ArrayList<CustomObject> array A are unsorted: ArrayList<CustomObject> array A中的对象未排序:

ID: [6, 3, 2, 5, 4, 1]

From another part of a program I received array B . 从程序的另一部分,我收到了array B Need to sort objects in array A by property int ID in the way array B is sorted: 需要通过属性int IDarray A的对象进行排序,方法是对array B进行排序:

[3, 6, 4, 5, 1, 2]

So array A should be finally sorted like: 因此, array A最终排序应为:

[3, 6, 4, 5, 1, 2]

So the objects in array A are sorted by ID exactly like items in array B are. 因此, array A中的对象按ID排序,就像array B中的项目一样。

What would be the best way to implement it? 实施它的最佳方法是什么?

Try this. 尝试这个。

    List<CustomObject> a = Arrays.asList(
        new CustomObject(6),
        new CustomObject(3),
        new CustomObject(2),
        new CustomObject(5),
        new CustomObject(4),
        new CustomObject(1)
    );
    System.out.println(a);
    int[] b = {4, 6, 5, 3, 1, 2};
    Map<Integer, Integer> map = new HashMap<>();
    for (int i = 0; i < b.length; ++i)
        map.put(b[i], i);
    // for Java8
    // Collections.sort(a, (l, r) -> map.get(l.id) - map.get(r.id));
    Collections.sort(a, new Comparator<CustomObject>() {
        @Override
        public int compare(CustomObject l, CustomObject r) {
            return map.get(l.id) - map.get(r.id);
        }
    });
    System.out.println(a);

This may helpful to you : 这可能对您有帮助:

public static void main(String[] args) {
    HashMap<Integer, Integer> myMap = new HashMap();
    int arrayA[] = {6, 3, 2, 5, 4, 1};
    int arrayB[] = {3, 6, 4, 5, 1, 2};
    for (int i = 0; i < arrayA.length; i++) {
        if (myMap.containsKey(arrayA[i])) {
            int pValue = myMap.get(arrayA[i]);
            pValue++;
            myMap.put(arrayA[i], pValue);
        } else if (!myMap.containsKey(arrayA[i])) {
            myMap.put(arrayA[i], 1);
        }
    }
    int l = 0;
    for (int i = 0; i < arrayB.length; i++) {
        if (myMap.containsKey(arrayB[i])) {
            int pValue = myMap.get(arrayB[i]);
            for (int k = 0; k < pValue; k++) {
                arrayA[l++] = arrayB[i];
            }
        }
    }
    System.out.println(Arrays.toString(arrayA));

}

Out-put : 输出:

[3, 6, 4, 5, 1, 2]

Approach 途径

  1. First create a comparator object initialize it with the required arrayOrder 首先创建一个比较器对象,使用所需的arrayOrder对其进行初始化
  2. Call the Collection.sort with the comparator object 使用比较器对象调用Collection.sort

I understand that this could be an over kill for a simple solution mentioned above. 我了解这对于上面提到的简单解决方案可能是一个致命的挑战。 but I have tried to exploit Comparator interface from the java.utils 但是我试图从java.utils利用Comparator接口

import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;

class CustomerCompartor implements Comparator<Customer> {
    @SuppressWarnings("rawtypes")
    ArrayList arrayOrder;

    @SuppressWarnings("unchecked")
    public CustomerCompartor(Integer[] arrayOrderInt) {
        this.arrayOrder = new ArrayList(Arrays.asList(arrayOrderInt));

    }

    public int compare(Customer cust1, Customer cust2) {

        int cust1Order = this.arrayOrder.indexOf(cust1.getId());
        int cust2Order = this.arrayOrder.indexOf(cust2.getId());
        return cust1Order - cust2Order;
    }
}

class Customer {

    int id;

    public Customer(int id) {
        this.id = id;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
}

public class ArrayOrder {
    public static void main(String[] args) {
        List<Customer> customerArray = new ArrayList<Customer>();

        customerArray.add(new Customer(1));
        customerArray.add(new Customer(2));
        customerArray.add(new Customer(3));
        customerArray.add(new Customer(4));
        customerArray.add(new Customer(5));
        customerArray.add(new Customer(6));

        for (Customer cust : customerArray) {
            System.out.println(cust.getId());
        }

        Integer[] arrayOrder = new Integer[] { 3, 6, 4, 5, 1, 2 };

        CustomerCompartor comparator = new CustomerCompartor(arrayOrder);
        Collections.sort(customerArray, comparator);

        for (Customer cust : customerArray) {
            System.out.println(cust.getId());
        }

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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