首页 > 解决方案 > 按数组内对象内的内容对数组进行排序

问题描述

@Override
    public String createExport(ArrayList<Integer> allIdList) {

        ArrayList<Answer> allAnswer = new ArrayList<>();
        for(int elem : vvtList) {
            allAnswer.addAll(findById(elem).getAnswer());
        }

        for(Answer elem : allAnswer) {
            allAnswer.sort(elem.getFirstSortId());    //Doesn't work
            allAnswer.sort(elem.getSecondSortId());   //Doesn't work
        }

        return allAnswer.toString;

    }

我创建了自己的类 Anwser。它包含两个整数,firstSortId 和 secondSortId。我想按它们对 allAnswer-ArrayList 进行排序。此刻的阵法列表一片混乱。

firstSortId 76 secondSortId 2
firstSortId 57 secondSortId 1
firstSortId 36 secondSortId 2

结果应该是

firstSortId 57 secondSortId 1
firstSortId 36 secondSortId 2
firstSortId 76 secondSortId 2

如何将数组按一个排序,然后按其中的两个整数排序?

标签: java

解决方案


推荐阅读