对名字和姓氏数组进行排序

时间:2015-02-12 14:13:30

标签: java arrays sorting

我正在制作一个程序,我需要在飞机上为座位表制作方法排序这个名字和姓氏列表。它是一个二维数组,第一列具有第一个名称,第二列具有第一列中相应人员的姓氏。我需要按照姓氏,名字的字母顺序对此进行排序,但我无法弄清楚如何对姓氏进行排序,然后保留相应的姓氏。

这是我的代码:

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

按顺序:用户输入36个姓名,高度以及此人是否想要提出座位请求。我让它复制了数组2另一个数组,然后我按字母顺序排列了姓氏。现在我无法弄清楚如何获得按字母顺序排列的姓氏的相应名字。

2 个答案:

答案 0 :(得分:1)

试试这种: -

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

答案 1 :(得分:0)

您应该使用排序算法。你可以在这里发现它们:Sorting algorithms。每个方法都有一些示例代码。

比较字符串时要小心,你应该使用方法compareTo()方法,如下所示:

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");

如果您选择创建另一个数组来存储已排序的数组,则juste会创建一个具有相同参数的数组。在这里,创建这个:

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

对于您的示例,您还应该根据排序条件同时对 firstAndLastNames seatRequest 数组进行排序。