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?

3
  • I strongly suggest that you review all answers in the linked post. Commented Jan 13, 2020 at 15:40
  • allAnswer.sort(Comparator.comparing(Answer::getFirstSortId).thenComparing(Answer::getSecondSortId)); Commented Jan 13, 2020 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 Commented Jan 13, 2020 at 15:55

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.