Sort users

Report a typo

We have the User class shown in the following code snippet and we want to sort users by their names in the lexicographic order.

class User {
    private String name;

    public User(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    @Override
    public String toString() {
        return name;
    }
}

Implement UserComparator that we will use to sort users. You don't need to read or write anything from or to the console, just implement the method.

Sample Input 1:

Mike Ginger Alice Bob

Sample Output 1:

Alice Bob Ginger Mike

Sample Input 2:

microprogrammer Moses Chloe user

Sample Output 2:

Chloe Moses microprogrammer user
Write a program in Java 17
import java.util.Comparator;

class User {
private String name;

public User(String name) {
this.name = name;
}

public String getName() {
return name;
}

@Override
public String toString() {
return name;
}
}

class UserComparator implements Comparator<User> {

@Override
public int compare(User user1, User user2) {
// your code here
return 0;
}
}
___

Create a free account to access the full topic