Garrett
Garrett

Reputation: 31

Sorting First and Last Names Array

I am making a program and I need to sort this list of first and last names for a seating chart making method on an airplane. It is a 2 dimensional array the first column has the first name, and the second column has the last name of the corresponding person in the first column. I need to sort this in alphabetical order by last name, first name but I cannot figure out how to sort the last name and then keep the corresponding last name.

This is my Code:

package assignment_6_1;
import java.util.Scanner;
import java.util.Arrays;
public class Assignment_6_1 {

    public static void main(String[] args) {
        //Create a Scanner
        Scanner input = new Scanner(System.in);
        //Create int & string []
        String[][]firstAndLastNames = new String[36][2];
        int[]heightOfPerson = new int[36];
        String[][]seatingChartDiagram = new String[9][4];
        int[][]seatRequest = new int [36][2];

        //Gather list of names and heights
        for(int i=0; i<heightOfPerson.length; i++)
        {

            //Get first name
            System.out.println("Enter Your First Name: ");
            firstAndLastNames[i][0] = input.next();

            //Get last name
            System.out.println("Enter Your Last Name: ");
            firstAndLastNames[i][1] = input.next();

            //Get height in inches
            System.out.println("Enter your height (Iches) : ");
            heightOfPerson[i] = input.nextInt();

            //Is there a seat request or not
            System.out.println("Do you want to request a seat ?");
            String ifSeatRequest = input.next();

            //Get seat request
            if(ifSeatRequest.equals("Yes") || ifSeatRequest.equals("yes"))
            {
                System.out.println("Enter the seat row you want: ");
                seatRequest[i][0] = input.nextInt();
                System.out.println("Enter the seat number you want in that row: ");
                seatRequest[i][1] = input.nextInt();
            }
            else
            {
                //set the request colum for that person to 0 for no request
                seatRequest[i][0] = 0;
                seatRequest[i][1] = 0;
            }
            //Prints passenger manifest when list is full
            if(firstAndLastNames[35][1] != null){
            System.out.println("All Seats are Filled");
            break;}
        }
         String[]firstNameSort = new String[36];
        //put the first names into another array to sort
        for(int j=0; j<heightOfPerson.length; j++)
        {
         firstNameSort[j] = firstAndLastNames[j][1];   
        }
        //Alphabetize first name list
        Arrays.sort(firstNameSort);
        String[][]firstNameAlphLast = new String[36][2];
         String[][]nameAlph = new String[36][2];
    }
}

In order: It takes in user input for 36 names, heights, and if the person wants to make a seating request. I made it copy the array 2 another array, and then I alphabetized the last names. Now i can't figure out how to get the corresponding first names for the alphabetized last names.

Upvotes: 2

Views: 3812

Answers (2)

Soma
Soma

Reputation: 857

You should use a sorting algorithm. You can discover them here : Sorting algorithms. You have some sample code for each method.

Be careful when comparing strings, you should use the method compareTo() method as follows :

string1.compareTo(string2);

Results:
-1 if string1 is before string2 in the alphabetical order : "Hello".compareTo("World");
0  if string1 is exactly the same than string2 : "Hello".compareTo("Hello);
1  if string1 is after string2 in the alphabetical order : "World.compareTo("Hello");

If you choose to create another array to store your sorted array, juste create one with the same parameter. Here, create this one :

String[][] firstAndLastNamesSorted = new String[36][2];

For your example, you should also sort both firstAndLastNames and seatRequest arrays at the same time according to your sorting criteria.

Upvotes: 0

Paresh
Paresh

Reputation: 156

Try this sort:-

Arrays.sort(firstAndLastNames, new Comparator<String[]>() {
        @Override
        public int compare(final String[] a1, final String[] a2) {
            return a1[1].compareTo(a2[1]) + a1[0].compareTo(a2[0]);
        }
    });

Upvotes: 1

Related Questions