0

So I'm trying to lexicographically sort this collection but with no success. The same unsorted collection is in the input and the output of the sort method.

class Person {
    
    private String privateName;
    private String lastName;
    
    public Person(String privateName, String lastName) {
        this.privateName = privateName;
        this.lastName = lastName;
    }
    
    public String toString() {
        return privateName + " " + lastName;
    }
}

class Main {
    public static void main(String[] args) {
        Collection<Person> people = new ArrayList<>();
        
        people.add(new Person("aaa", "hhh"));
        people.add(new Person("aaa", "aaa"));
        people.add(new Person("aaa", "uuu"));

        Arrays.sort(people.toArray(), Comparator.comparing(Object::toString));
    }
}

The order of the elements in the output collection: "aaa hhh" -> "aaa aaa" -> "aaa uuu"

While I want it to be: "aaa aaa" -> "aaa hhh" -> "aaa uuu"

Can somebody tell me why?

1
  • 2
    Change Collection<Person> people to List<Person> people which will let you use people.sort(Comparator.comparing(Object::toString)). Currently you are sorting array created via people.toArray(), not people list itself. Commented Nov 23, 2022 at 12:29

2 Answers 2

4

Your problem is that you are converting your Collection/ArrayList to an array and then sort that Array.

Sorting that Array will have no effect on the original ArrayList.

If you want to sort your List you first need to declare it as a List, because Collections themself have no predefined order and therefor no sort method for them exists, and then in a second step use the corresponding method to sort that list:

public static void main(final String[] args) {
    final List<Person> people = new ArrayList<>();
    
    people.add(new Person("aaa", "hhh"));
    people.add(new Person("aaa", "aaa"));
    people.add(new Person("aaa", "uuu"));

    Collections.sort(people, Comparator.comparing(Person::toString));
    
    System.out.println(people);
    
}

Will output

[aaaaaa, aaahhh, aaauuu]

Sign up to request clarification or add additional context in comments.

2 Comments

Slightly shorter: people.sort(Comparator.comparing(Person::toString)).
@user16076953 check my answer as well bellow to get some more insight in collections and arrays.
0

Plot twist

The following

public static void main(String[] args) {

            Person[] peopleArray = {
                    new Person("aaa", "hhh"),
                    new Person("aaa", "aaa"),
                    new Person("aaa", "uuu")
            };

            Collection<Person> people = Arrays.asList(peopleArray);

            Arrays.sort(peopleArray, Comparator.comparing(Object::toString));

            System.out.println(people);
        }

Will print you the [aaa aaa, aaa hhh, aaa uuu] which is what you expect.

If you don't sort it and remove the line Arrays.sort(peopleArray, Comparator.comparing(Object::toString)); it will print you the [aaa hhh, aaa aaa, aaa uuu].

Reason why this would work -> Arrays.asList() is backed directly by the array, so when you sort the array you see the same sorting in the collection .

java.util.ArrayList() on the other side is not backed directly by any array that stores the elements since the ArrayList supports dynamic list size.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.