0
在ArrayList<ArrayList<Double>> selected
中存储了5个条目。ArrayList中的订购条目
rank = [1.0, 2.0, 3.1, 1.2, 2.1]
cd = [6.2, 5.2, 7.1, 8.0, 1.1]
我需要订购这些条目,首先,通过rank
和,其次降序(即3.1,由cd
,2.1:rank
和cd
- 这些条目中的每一个由两个参数指定,2.0,1.2,1.1)。第二次订购(按cd
)必须应用于已经按照rank
订购的条目。
ArrayList<Double> rank = new ArrayList<Double>();
ArrayList<Double> cd = new ArrayList<Double>();
ArrayList<ArrayList<Double>> selected = new ArrayList<ArrayList<Double>>();
for (int i=0; i<len; i++) {
rank.add(getRank(i));
cd.add(getCub_len(i));
}
selected.add(0,rank);
selected.add(1,cd);
Comparator<ArrayList<Double>> comparatorRank = new Comparator<ArrayList<Double>>()
{
public int compare(ArrayList<Double> a, ArrayList<Double> b)
{
return (int) (a.get(0) - b.get(0));
}
};
Comparator<ArrayList<Double>> comparatorCD = new Comparator<ArrayList<Double>>()
{
public int compare(ArrayList<Double> a, ArrayList<Double> b)
{
return (int) (a.get(1) - b.get(1));
}
};
Collections.sort(selected, comparatorRank);
Collections.sort(selected, comparatorCD);
问题是我不知道如何在订购之前获取已分配给条目的ID。例如,这是ID的无序序列:1,2,3,4,5,这是订购后的ID序列:5,3,4,1,2。如何获得这些ID?
好像你需要存储单个对象的级别和代码,这样就可以有一个对象列表的类。然后你只需定义你的compareTo类来使用rank作为字段进行排序。 – 2013-03-27 19:22:28
@Kevin Crowell:你能举一些例子吗?谢谢。 – 2013-03-27 19:25:58
检查此问题的接受答案:http://stackoverflow.com/questions/3718383/java-class-implements-comparable – 2013-03-27 22:06:03