I have a collection with data like:
John eats candy
Richard runs around in the morning
Hannah drinks water
I divided each word, added it to the array and got:
[John, eats, candy]
[Richard, runs, around, in, the, morning]
[Hannah, drinks, water]
The output should be (the second words are sorted alphabetically)
[Hannah, drinks, water]
[John, eats, candy]
[Richard, runs, around, in, the, morning]
Here is my code:
List<List<String>> mainList = new ArrayList<>();
List<String> list = new ArrayList<>();
for (String s : array) {
String[] x = s.split(" ");
for (int i = 0; i < x.length; i++) {
list.add(x[i]);
}
mainList.add(list);
list = new ArrayList<>();
}
But I do not know what to do next. How to implement a comparison of the elements and, based on this, replace the lines. Is it right at all to divide strings into arrays to get access to the second element? I will be happy, even if you just advise something.