2

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
Digvijaysinh Gohil
  • 1,367
  • 2
  • 15
  • 30
  • You can find more information related at https://stackoverflow.com/questions/1946668/sorting-using-comparator-descending-order-user-defined-classes – Jigar Shah Nov 24 '17 at 11:17
  • Thank you for the information –  Nov 24 '17 at 11:20

2 Answers2

0

If you want to output the three smallest people's you should use:

Collections.sort(peopleFile,Comparator.comparingInt(People::getMaximumLength));

instead of:

Collections.sort(peopleFile,Comparator.comparingInt(People::getMaximumLength).reversed());

And you have to sort and then limit. Do not try to limit and then sort.

See the example below for a working example:

public static void main(String[] args) {

    class People {
        String peopleName;
        int maximumLength;

        public People(String peopleName, int maximumLength) {
            this.peopleName = peopleName;
            this.maximumLength = maximumLength;
        }

        public int getMaximumLength() {
            return maximumLength;
        }

        @Override
        public String toString() {
            return "{" +
                    "peopleName='" + peopleName + '\'' +
                    ", maximumLength=" + maximumLength +
                    '}';
        }
    }

    List<People> people = Arrays.asList(new People("John", 175), new People("Jane", 168),
            new People("James", 189), new People("Mary", 167),
            new People("tim", 162));
    people.stream().sorted(Comparator.comparingInt(People::getMaximumLength)).limit(3).forEach(System.out::println);
    System.out.println();
    people.stream().sorted(Comparator.comparingInt(People::getMaximumLength).reversed()).limit(3).forEach(System.out::println);
}

This will print out the 3 shortest first and after the break the 3 tallest:

{peopleName='tim', maximumLength=162}
{peopleName='Mary', maximumLength=167}
{peopleName='Jane', maximumLength=168}

{peopleName='James', maximumLength=189}
{peopleName='John', maximumLength=175}
{peopleName='Jane', maximumLength=168}
gil.fernandes
  • 12,978
  • 5
  • 63
  • 76
0

Maybe you missed assigning value like the following example

List result = list.stream().sorted((o1, o2)->o1.getItem().getValue().
                               compareTo(o2.getItem().getValue())).
                               collect(Collectors.toList());

reference

Sorting a list with stream.sorted() in Java

Jigar Shah
  • 470
  • 1
  • 6
  • 13