简体   繁体   English

如何基于整数数组排序对象集

[英]how to order set of objects based on array of integers

I have a class called Variable 我有一堂课叫做Variable

Class Variable{ private String name; private int[] domain; //...etc} 

which represents variable in specific structure (constraint satisfaction problem). 它表示特定结构中的变量(约束满足问题)。

I have instantiated set of variables in ArrayList< Variable > and filled up an array of integers. 我在ArrayList <Variable>中实例化了一组变量,并填充了一个整数数组。

 ArrayList<Variable> vars=new ArrayList<Variable>();
 Variable a=new Variable("A",new int[]{1,2});
 vars.add(a);
 // Define all variables;
 int[] cons=new int[vars.size()];
  for(int i=0;i<cons.length;i++)
    cons[i]=number_of_constraints(vars.get(i));
    // cons contains number of involved constraints for each variable

Now I need to sort them descending based on the number of constraints. 现在,我需要根据约束的数量对它们进行降序排序。

In other words: Given list of Objects [(A,{1,2}) , (B,{3,4}) , (C,{5,6}) ] and an array of integers cons={1,2,0} how to sort the list of objects descending based on the array of integers? 换句话说:给定对象列表[(A,{1,2}) , (B,{3,4}) , (C,{5,6}) ]和整数cons={1,2,0}如何根据整数数组对降序的对象列表进行排序?

If you would like to keep Class Variable intact, the following code will sort the given vars outside: 如果您希望保持Class Variable不变,则以下代码将给定的vars排序在外面:

   Collections.sort(vars, new Comparator<Variable>() {
       public int compare(Variable var1, Variable var2) {
           return var2.number_of_constraints() - var1.number_of_constraints();
    }});

If you can change Class Variable , let it implement interface Comparable : 如果可以更改Class Variable ,则让它实现Comparable接口:

class Variable implements Comparable<Variable> {
    //...

    public int compareTo(Variable other) {
        return this.number_of_constraints() -
               other.number_of_constraints();
    }
}

Then you can sort vars by: 然后您可以按以下方式对vars进行排序:

Collections.sort(vars);

Use a sorted collection like a TreeSet 使用排序后的集合,例如TreeSet

class Variable {

    private String name;
    private int[] domain;
};
final Set<Variable> variables = new TreeSet<Variable>( new Comparator<Variable>() {

    public int compare(Variable o1, Variable o2) {
        //Do comparison here
        //return -1 if o1 is less than o2
        //1 if o1 is greater than o2
        //0 if they are the same
    }
});

Now you have a sorted Set of your Variable s. 现在您有了一个Variable Set的排序Set This is guaranteed to always be sorted. 这保证总是被排序。

Your Variable class should implement the Comparable interface, When it does you should implement the compareTo method. 您的Variable类应实现Comparable接口,在实现时应实现compareTo方法。

After that you can sort it by calling the Collection.sort method. 之后,您可以通过调用Collection.sort方法对其进行排序。

If you want to sort by a permutation if your indexes that's just a matter of creating a new ArrayList and mapping each index to the new index (using a for loop) 如果您想按排列对索引进行排序,那么只需创建一个新的ArrayList并将每个索引映射到新索引即可(使用for循环)

Here is such a (generic) method 这是一种(通用)方法

public static <T> ArrayList<T> permutate(ArrayList<T> origin,int[] permutation){
        ArrayList<T> result = new ArrayList<T>(permutation.length);
        for(int j=0;j<permutation.length;j++){
            result.add(null);
        }
        for(int i=0;i<permutation.length;i++){
            result.set(i, origin.get(permutation[i]));
        }
        return result;
    }

You can do myArrayList= permutate(myArrayList, new int{1,2,3}); 您可以执行myArrayList= permutate(myArrayList, new int{1,2,3});

Here is example usage in a more basic use case (integers): 这是一个更基本的用例(整数)中的示例用法:

public static void main(String... args){
    ArrayList<Integer> origin = new ArrayList<>(4);
    origin.add(1);
    origin.add(2);
    origin.add(3);
    origin.add(4);

    int[] per = new int[]{2,1,3,0};
    origin = permutate(origin,per);
    System.out.println(Arrays.toString(origin.toArray())); //prints [3,2,4,1], your permutation
}

As far as a Variable contains numOfConstraints , according to your code, you can make your Variable class implement Comparable interface, like 根据代码,只要Variable包含numOfConstraints ,就可以使Variable类实现Comparable接口,例如

public class Variuable implements Comparable<Variable> {

    private int numOfConstraints;

    public int compareTo(Variable other){
        if(this == other) { return 0; }
        return (numOfConstraints == other.numOfConstraint) ? 0 : ((numOfConstraints > other.numOfConstraint) ? 1 : -1);
    }

}

And then use the utility method java.util.Collections.sort(vars); 然后使用实用程序方法java.util.Collections.sort(vars); , that's it. , 而已。

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

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