0

I'm making a program that sorts names based on last name, but user enters a full name such as "John Smith" and I can't get it to sort by the name after the space.

It seems to only want to sort by the first name entered and ignore the last, but I want to sort by last name

        System.out.println("Welcome to name sorter program");

    //Set max names to 10
    String [] names = new String[10];
    String temp;
    for (int i = 0; i < names.length; i++)
    {
        System.out.println("Enter person " +(i + 1));
        //Name entered is assigned to each array slot
        names[i] = userInput.nextLine();
    }
        for (int i = 0; i < names.length; i++)
    {
        for (int j = i + 1; j < names.length; j++ )
        {
            if (names[i].compareTo(names[j]) > 0)
            {
                temp = names[i];
                names[i] = names[j];
                names[j] = temp;
            }
        }
    }
    System.out.println("Sorted Names Are...");
    for (int i = 0; i < names.length - 1; i++)
    {
        System.out.println(names[i]);
    }
        System.out.println(names[names.length - 1]);
}
12
  • Okay. Why can't you? Is there a question you meant to ask here? Commented Sep 27, 2016 at 3:22
  • Because my code seems to only sort by the first name entered Commented Sep 27, 2016 at 3:23
  • Yes, that is what your code does. Why can't you change it so it does what you want it to do? Commented Sep 27, 2016 at 3:23
  • I guess I don't know how to do that Commented Sep 27, 2016 at 3:26
  • Split the string, and only compare the part after the space. Commented Sep 27, 2016 at 3:29

2 Answers 2

1

My answer is almost the same of @BahramdunAdil:

public class Main {

    public static void main(String[] args) {

        final int MAX = 10;

        Scanner userInput = new Scanner(System.in);

        List<Person> names = new ArrayList<Person>();

        System.out.println("Welcome to name sorter program!\n");

        for (int i = 0; i < MAX; i++) {
            System.out.println("Enter person " + (i + 1) + ":");
            names.add(new Person(userInput.nextLine()));
        }

        userInput.close();

        Collections.sort(names);

        System.out.println("\nSorted Names Are...");

        for (Person name : names)
            System.out.println(name);
    }

}

i.e, you can turn your code simpler creating a Person Class that implements Comparable:

public class Person implements Comparable<Person> {
    private String firstName;
    private String lastName;

    public Person(String name) {
        this.firstName = name.split(" ")[0];
        this.lastName = name.split(" ")[1];
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    @Override
    public int compareTo(Person o) {
        return lastName.compareTo(o.getLastName());
    }

    @Override
    public String toString() {
        return firstName + " " + lastName;
    }
}

Input:

Welcome to name sorter program!

Enter person 1:
Bill Gates
Enter person 2:
Amancio Ortega
Enter person 3:
Warren Buffett
Enter person 4:
Carlos Slim Helu
Enter person 5:
Jeff Bezos
Enter person 6:
Mark Zuckerberg
Enter person 7:
Larry Ellison
Enter person 8:
Michael Bloomberg
Enter person 9:
Charles Koch
Enter person 10:
David Koch

Output:

Sorted Names Are...
Jeff Bezos
Michael Bloomberg
Warren Buffett
Larry Ellison
Bill Gates
Charles Koch
David Koch
Amancio Ortega
Carlos Slim
Mark Zuckerberg
Sign up to request clarification or add additional context in comments.

Comments

1

You can do it with the Collections, here is an example: Create a Person class and implement the Comparable interface and then do comparison in the compareTo() method...

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    List<Person> persons = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        System.out.println("Enter person " +(i + 1));
        String line = in.nextLine();
        // split the name with apace
        String[] array = line.split(" ");
        // crate a new Person
        Person person = new Person(array[0], array[1]);
        // add to the Person list
        persons.add(person);
    }

    // now sort it
    Collections.sort(persons);

    // after sort
    System.out.println("persons = " + persons);
}

static class Person implements Comparable<Person> {
    private String firstName;
    private String lastName;

    public Person(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    @Override
    public int compareTo(Person o) {
        return lastName.compareTo(o.lastName);
    }

    @Override
    public String toString() {
        return "Person{" + "firstName='" + firstName + '\'' + ", lastName='" + lastName + '\'' + '}';
    }
}

Comments

Your Answer

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