我有一个类叫做变量
Class Variable{ private String name; private int[] domain; //...etc}
表示特定结构中的变量(约束满足问题)。
我在 ArrayList
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
现在我需要根据约束的数量对它们进行降序排序。
换句话说:给定对象列表 [(A,{1,2}) , (B,{3,4}) , (C,{5,6}) ]
和整数数组 cons={1,2,0}
如何根据整数数组对对象列表进行降序排序?
最佳答案
使用像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
}
});
现在您有一个已排序的 Set
的 Variable
。这保证始终排序。
关于java - 如何根据整数数组对对象集进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15039941/