Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 按另一个数组对对象的Arraylist排序_Java_Sorting_Arraylist - Fatal编程技术网

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

Java 按另一个数组对对象的Arraylist排序,java,sorting,arraylist,Java,Sorting,Arraylist,ArrayListarrayA中的对象未排序: ID: [6, 3, 2, 5, 4, 1] 我从程序的另一部分接收到数组B。需要按属性对数组A中的对象进行排序int ID,排序方式为数组B: [3, 6, 4, 5, 1, 2] 因此,数组A最终应按如下方式排序: [3, 6, 4, 5, 1, 2] 因此数组A中的对象按ID排序,与数组B中的项目完全相同 实现它的最佳方法是什么?试试这个 List<CustomObject> a = Arrays.asList(

ArrayList
arrayA
中的对象未排序:

ID: [6, 3, 2, 5, 4, 1]
我从程序的另一部分接收到
数组B
。需要按属性对
数组A
中的对象进行排序
int ID
,排序方式为
数组B

[3, 6, 4, 5, 1, 2]
因此,
数组A
最终应按如下方式排序:

[3, 6, 4, 5, 1, 2]
因此
数组A
中的对象按ID排序,与
数组B
中的项目完全相同

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

试试这个

    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);
List a=Arrays.asList(
新自定义对象(6),
新自定义对象(3),
新自定义对象(2),
新自定义对象(5),
新自定义对象(4),
新自定义对象(1)
);
系统输出打印项次(a);
int[]b={4,6,5,3,1,2};
Map Map=newhashmap();
对于(int i=0;imap.get(l.id)-map.get(r.id));
Collections.sort(一个新的Comparator(){
@凌驾
公共整数比较(CustomObject l、CustomObject r){
返回map.get(l.id)-map.get(r.id);
}
});
系统输出打印项次(a);

这可能对您有所帮助:

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

}
接近

  • 首先创建一个比较器对象,并使用所需的 安排人
  • 使用comparator对象调用Collection.sort
  • 我理解,对于上面提到的一个简单的解决方案来说,这可能是一个过度的杀伤力。但是我尝试从
    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());
            }
    
        }
    }
    
    import java.util.List;
    导入java.util.ArrayList;
    导入java.util.array;
    导入java.util.Collections;
    导入java.util.Comparator;
    类CustomerCompartor实现比较器{
    @抑制警告(“原始类型”)
    ArrayList arrayOrder;
    @抑制警告(“未选中”)
    公共客户合作伙伴(整数[]数组派生){
    this.arrayOrder=newarraylist(Arrays.asList(arrayOrderInt));
    }
    公共整数比较(客户客户1、客户客户2){
    int cust1Order=this.arrayOrder.indexOf(cust1.getId());
    int cust2Order=this.arrayOrder.indexOf(cust2.getId());
    返回cust1订单-cust2Order;
    }
    }
    类客户{
    int-id;
    公共客户(内部id){
    this.id=id;
    }
    公共int getId(){
    返回id;
    }
    公共无效集合id(内部id){
    this.id=id;
    }
    }
    公共类安排人{
    公共静态void main(字符串[]args){
    List customerArray=new ArrayList();
    customerArray.add(新客户(1));
    customerArray.add(新客户(2));
    customerArray.add(新客户(3));
    customerArray.add(新客户(4));
    customerArray.add(新客户(5));
    customerArray.add(新客户(6));
    用于(客户:customerArray){
    System.out.println(cust.getId());
    }
    整数[]数组排序器=新整数[]{3,6,4,5,1,2};
    CustomerCompartor comparator=新的CustomerCompartor(arrayOrder);
    集合。排序(customerArray、comparator);
    用于(客户:customerArray){
    System.out.println(cust.getId());
    }
    }
    }
    
    什么是基准排序?@cricket\u 007,我需要完成的最后一个订单外观您是否尝试根据某些内容进行排序?你的比较函数是什么?你的结果不是上升或下降,所以很难得到你想要的。@cricket_007,是的,它不仅仅是asc或desc或其他什么。。。这是我从另一个函数收到的标识符数组,需要应用它们来对ArrayList中的对象进行排序。所以这个ArrayList是按照它的ID字段进行排序的,数组的设置方式是这样的:我收到了一个“苹果”、“橘子”和“香蕉”数组,然后按这个顺序把它们放在我的包里。但是妈妈过来说:“嘿,你应该把它们像:“橙色”->“苹果”->“香蕉”。谢谢,这似乎是一个解决办法。但是你能不能不使用lambdas重新修改它?Android不支持Java 1.8,这很遗憾Cricket007删除了我的
    Android
    标签,从问题“怎么做”(l,r)->map.get(l.id)-map.get(r.id)“work???@saka1029第二个参数类型错误。发现:…CustomObject,必需的java.util.ComparatorIs第一个参数类型<代码>列表<代码>?最终工作与建议的完全相同,非常感谢!在同一元素上重复
    包含
    ,成本不必要。
    
    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());
            }
    
        }
    }