2

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.

7
  • 1
    Why don't you create a Person object with three properties (lastName, firstName and height) ? Commented Feb 12, 2015 at 14:17
  • @FlorentBayle i have never heard of a person object what is that Commented Feb 12, 2015 at 14:19
  • @FlorentBayle all they talked about in the leson was string and int arrays Commented Feb 12, 2015 at 14:20
  • hmmm, I can see why its hard, since you can't use any other data types nor creating new objects..... but the answer is simple just don't use Array.sort instead create your own sorting method. :) Commented Feb 12, 2015 at 14:22
  • I suggest you have a look at this: docs.oracle.com/javase/tutorial/java/javaOO/constructors.html (regarding your query about person objects.) Commented Feb 12, 2015 at 14:54

2 Answers 2

1

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]);
        }
    });
Sign up to request clarification or add additional context in comments.

1 Comment

I didn't know that Arrays.sort could accept a comparator.. Thank you :)
0

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.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.