I want to print out the three smallest people by length, ordered by the smallest first and it needs to print out the name of each person on a newline along with the maximum length in centimetres from a text file.
So far I have created a comparator with Collections.sort to sort out the Arrays I have created within my file, and to order them.
Comparator:
Collections.sort(peopleFile,Comparator.comparingInt(People::getMaximumLength).reversed());
Arrays:
List<People> peopleFile = new ArrayList<>();
String[] tokenSize = fileRead.split(":");
String peopleName = tokenSize[0];
int maximumLength = Integer.parseInt(tokenSize[1]);
Print:
System.out.println("The three smallest people are: ");
peopleFile.stream().limit(3).forEach(System.out::println);
Output:
The three smallest people are:
David Lee, Length = 150 centimetres
Amber Jones, Length = 142 centimetres
Mandy Stones, Length = 152 centimetres
The problem is that it doesn't output the largest people, it just prints out the order in the text file.
This is how my output should look like:
The three smallest people are:
Amber Jones, Length = 142 centimetres
Samantha Lee, Length = 144 centimetres
Andre Bishop, Length = 145 centimetres