Design and develop an algorithm, function, method, utility, command or component that sorts a multi-column contact list (contain last name, first name, middle name). You may use any appropriate data structure of choice to hold your data.
Implement your solution as part of a program (written in JAVA) that displays list, before sorted, after sorted. Refer to the Sample Unsorted Input Data and the Sample Sorted Output Data below to see how the list be transformed by the sort process.
The output should be ordered by Last Name. First Name must be ordered within Last Name and the Middle Name should be ordered within First Name. All the sorting be performed in ascending order.
If use a pre-existing algorithm, identify Library, OS, individual, etc. give full attribution to source.
Sample Unsorted Input Data:
Smith, John Vince
Doe, Jane Marie
Doe, Sally Mae
Smith, Robert Anthony
Doe, Sally Maria
Sample Sorted Output Data:
Doe, Jane Marie
Doe, Sally Mae
Doe, Sally Maria
Smith, John Vince
Smith, Robert Anthony
import java.util.Arrays;
import java.util.Scanner;
public class Main
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int n;
System.out.println("Enter number of people: ");
n=in.nextInt();
String [] names=new String [n];
System.out.println("Enter "+n+" names: ");
for(int i=0;i<n;i++){
names[i]=in.next();
}
Arrays.sort(names);
System.out.println(Arrays.toString(names));
}
}
Comments
Leave a comment