按另一个数组排序对象的Arraylist

时间:2015-12-13 03:37:37

标签: java sorting arraylist

ArrayList<CustomObject> array A中的对象未分类:

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

我从节目的另一部分收到了array B。需要按array A排序的方式按int ID按属性array B对对象进行排序:

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

所以array A应该最终排序如下:

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

因此,array A中的对象按ID排序,与array B中的项目完全相同。

实施它的最佳方法是什么?

3 个答案:

答案 0 :(得分:2)

试试这个。

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

答案 1 :(得分:0)

这可能对您有所帮助:

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

}

输出:

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

答案 2 :(得分:0)

方法

  1. 首先创建一个比较器对象,用所需的值初始化它 arrayOrder
  2. 使用比较器对象
  3. 调用Collection.sort

    据我所知,对于上面提到的简单解决方案,这可能是过度杀戮。但我试图利用Comparator

    中的java.utils界面
    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());
            }
    
        }
    }