0
@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;

    }

I created my own class Anwser. It contains two Integers, firstSortId and secondSortId. I want to sort the allAnswer-ArrayList by them. At the moment the Array List is pure chaos.

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

The result should be

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

How can I sort the array by one, and then by two Integers inside them?

Jan Bürger
  • 111
  • 2
  • 9
  • I strongly suggest that you review **all** answers in the linked post. – PM 77-1 Jan 13 '20 at 15:40
  • `allAnswer.sort(Comparator.comparing(Answer::getFirstSortId).thenComparing(Answer::getSecondSortId));` – Hovercraft Full Of Eels Jan 13 '20 at 15:42
  • Thank you guys, ` allAnswer.sort((a1, a2) -> ((Integer) a1.getFirstSortId()).compareTo(a2.getFirstSortId())); allAnswer.sort((a1, a2) -> ((Integer) a1.getSecondSortId()).compareTo(a2.getSecondSortId())); ' did the trick. Not sure how to you the .thenComparing – Jan Bürger Jan 13 '20 at 15:55

0 Answers0