基于两个元素对2d arraylist进行排序

时间:2016-04-29 21:30:02

标签: java

我正在尝试根据第一个元素对2d Arraylist进行排序,并在比较两个元素是否相同时按第二个元素排序。

所以我的Arraylist看起来像

 [[1, 4, 0], [2, 5, 2], [3, 5, 3], [4, 3, 6]]

我希望它看起来像这样

 [[4, 3, 6], [1, 4, 0], [2, 5, 2], [3, 5, 3]]

这是我的ArrayList

ArrayList<ArrayList<Integer>> processes = new ArrayList<>();

这是我到目前为止使用选择排序

int smallInt;
int j;
int smallIntIndex;
for(int i = 1; i<=processes.size();i++){
    smallInt = processes.get(i-1).get(1);
    smallIntIndex = i-1;
    for(j=i;j<processes.size();j++){
        if(processes.get(j).get(1)==smallInt){
          //not exactly sure what goes in here
        }else if(processes.get(j).get(1)<smallInt){
            smallInt = processes.get(j).get(1);
            smallIntIndex = j;
        }
    }
    ArrayList<Integer> temp = processes.get(smallIntIndex);
    processes.set(smallIntIndex,processes.get(i-1));
    processes.set(i-1,temp);
}

1 个答案:

答案 0 :(得分:0)

标签(例如1或2或3 - 第一/第二/第三条目)是否来自受限制的集合(例如仅限数字)? 如果是这样(我认为是这样),您可以通过应用Radix sort来对它们进行排序。

相关问题