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]);
}