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

Java 对名字和姓氏数组进行排序,java,arrays,sorting,Java,Arrays,Sorting,我正在制作一个程序,我需要为飞机上的座位表制作方法对这个名字和姓氏列表进行排序。它是一个二维数组,第一列有名字,第二列有第一列中对应人员的姓氏。我需要按姓氏和名字的字母顺序排序,但我不知道如何排序姓氏,然后保留相应的姓氏 这是我的代码: package assignment_6_1; import java.util.Scanner; import java.util.Arrays; public class Assignment_6_1 { public static void ma

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

这是我的代码:

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];
    }
}
package assignment_6_1;
导入java.util.Scanner;
导入java.util.array;
公开课作业(6)(1){
公共静态void main(字符串[]args){
//创建扫描仪
扫描仪输入=新扫描仪(System.in);
//创建int和string[]
字符串[][]firstAndLastNames=新字符串[36][2];
int[]人的高度=新的int[36];
字符串[][]seatingChartDiagram=新字符串[9][4];
int[][]seatRequest=新int[36][2];
//收集姓名和身高列表
对于(int i=0;i尝试以下排序:-

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]);
        }
    });
Arrays.sort(firstnames和lastnames,newcomparator(){
@凌驾
公共整数比较(最终字符串[]a1,最终字符串[]a2){
返回a1[1]。compareTo(a2[1])+a1[0]。compareTo(a2[0]);
}
});

您应该使用排序算法。您可以在此处找到它们:。对于每个方法,您都有一些示例代码

比较字符串时要小心,应使用方法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");
如果选择创建另一个数组来存储已排序的数组,只需使用相同的参数创建一个即可。此处,创建此数组:

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

例如,您还应该根据排序标准同时对firstName和lastName以及seatRequest数组进行排序。

为什么不创建一个具有三个属性的
Person
对象(
lastName
firstName
height
)?@FlorentBayle我从来没有听说过人和物体是什么that@FlorentBayleleson中讨论的都是字符串和int-arrayshmmm,我知道为什么很难,因为您不能使用任何其他数据类型,也不能创建新对象……但答案很简单,就是不要使用Array.sort,而是创建自己的排序方法。:)我建议您看一下:(关于您对person对象的查询)我不知道array.sort可以接受比较器..谢谢:)