seteropere
seteropere

Reputation: 489

how to order set of objects based on array of integers

I have a class called 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> 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?

Upvotes: 1

Views: 246

Answers (4)

Boris the Spider
Boris the Spider

Reputation: 61168

Use a sorted collection like a 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 Variables. This is guaranteed to always be sorted.

Upvotes: 2

Hui Zheng
Hui Zheng

Reputation: 10222

If you would like to keep Class Variable intact, the following code will sort the given vars outside:

   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 implements Comparable<Variable> {
    //...

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

Then you can sort vars by:

Collections.sort(vars);

Upvotes: 2

Benjamin Gruenbaum
Benjamin Gruenbaum

Reputation: 276436

Your Variable class should implement the Comparable interface, When it does you should implement the compareTo method.

After that you can sort it by calling the Collection.sort method.

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)

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

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
}

Upvotes: 1

skuntsel
skuntsel

Reputation: 11742

As far as a Variable contains numOfConstraints, according to your code, you can make your Variable class implement Comparable interface, like

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);, that's it.

Upvotes: 1

Related Questions