Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java中,如何按第三列的升序对2D数组进行排序?_Java_Arrays_Sorting_Multidimensional Array - Fatal编程技术网

在java中,如何按第三列的升序对2D数组进行排序?

在java中,如何按第三列的升序对2D数组进行排序?,java,arrays,sorting,multidimensional-array,Java,Arrays,Sorting,Multidimensional Array,我的代码用于对员工列表进行排序,首先按部门(第一列)排序,然后按年龄(第三列)升序排序。我找了好几个小时都没找到。到目前为止,我的代码是: public class Company { public static void main(String[] args) { String[][] departmentList = new String[13][3]; departmentList[0][0] = "Accountin

我的代码用于对员工列表进行排序,首先按部门(第一列)排序,然后按年龄(第三列)升序排序。我找了好几个小时都没找到。到目前为止,我的代码是:

    public class Company
    {
    public static void main(String[] args)
    {
        String[][] departmentList = new String[13][3];
            departmentList[0][0] = "Accounting";
            departmentList[0][1] = "Counting Guru";
            departmentList[0][2] = "55";
            departmentList[1][0] = "Accounting";
            departmentList[1][1] = "Counting Pro";
            departmentList[1][2] = "45";
            departmentList[2][0] = "Accounting";
            departmentList[2][1] = "Counting Savvy";
            departmentList[2][2] = "40";
            departmentList[3][0] = "Accounting";
            departmentList[3][1] = "Counting Novice";
            departmentList[3][2] = "25";
            departmentList[4][0] = "Marketing";
            departmentList[4][1] = "Sales Guru";
            departmentList[4][2] = "50";
            departmentList[5][0] = "Marketing";
            departmentList[5][1] = "Sales Pro";
            departmentList[5][2] = "48";
            departmentList[6][0] = "Marketing";
            departmentList[6][1] = "Sales Savvy";
            departmentList[6][2] = "38";
            departmentList[7][0] = "Human Resources";
            departmentList[7][1] = "Hiring Guru";
            departmentList[7][2] = "58";
            departmentList[8][0] = "Human Resources";
            departmentList[8][1] = "Hiring Pro";
            departmentList[8][2] = "47";
            departmentList[9][0] = "Information Systems";
            departmentList[9][1] = "Hacking Pro";
            departmentList[9][2] = "46";
            departmentList[10][0] = "Information Systems";
            departmentList[10][1] = "Hacking Guru";
            departmentList[10][2] = "51";
            departmentList[11][0] = "Information Systems";
            departmentList[11][1] = "Hacking Savvy";
            departmentList[11][2] = "38";
            departmentList[12][0] = "Information Systems";
            departmentList[12][1] = "Hacking Novice";
            departmentList[12][2] = "23";

        for(int row = 0; row < departmentList.length; row++)
        {
            System.out.println(departmentList[row][0] + "\t" + departmentList[row][1] + "\t" + departmentList[row][2]);
        }
    }
}
上市公司
{
公共静态void main(字符串[]args)
{
字符串[][]部门列表=新字符串[13][3];
部门列表[0][0]=“会计”;
部门列表[0][1]=“计数大师”;
部门名单[0][2]=“55”;
部门列表[1][0]=“会计”;
部门列表[1][1]=“计数专业”;
部门名单[1][2]=“45”;
部门列表[2][0]=“会计”;
部门列表[2][1]=“计算能力”;
部门名单[2][2]=“40”;
部门列表[3][0]=“会计”;
部门列表[3][1]=“计数新手”;
部门名单[3][2]=“25”;
部门列表[4][0]=“营销”;
部门列表[4][1]=“销售大师”;
部门名单[4][2]=“50”;
部门列表[5][0]=“营销”;
部门列表[5][1]=“Sales Pro”;
部门名单[5][2]=“48”;
部门列表[6][0]=“营销”;
部门列表[6][1]=“销售悟性”;
部门名单[6][2]=“38”;
部门名单[7][0]=“人力资源”;
部门列表[7][1]=“招聘专家”;
部门名单[7][2]=“58”;
部门列表[8][0]=“人力资源”;
部门列表[8][1]=“招聘专业”;
部门名单[8][2]=“47”;
部门名单[9][0]=“信息系统”;
部门列表[9][1]=“黑客专业”;
部门名单[9][2]=“46”;
部门名单[10][0]=“信息系统”;
部门列表[10][1]=“黑客大师”;
部门名单[10][2]=“51”;
部门名单[11][0]=“信息系统”;
部门列表[11][1]=“黑客悟性”;
部门名单[11][2]=“38”;
部门名单[12][0]=“信息系统”;
部门列表[12][1]=“黑客新手”;
部门名单[12][2]=“23”;
对于(int row=0;row
我希望输出按部门打印列表,然后按年龄从最小到最大打印。非常感谢您的帮助。

您可以使用并提供自定义比较器:

Arrays.sort(departmentList, new Comparator<String[]>() {
        @Override
        public int compare(String[] o1, String[] o2) {
           int cmp =  o1[0].compareTo(o2[0]);
           return cmp != 0 ? cmp : o1[2].compareTo(o2[2]);
        }           
    });
你喜欢这样吗

您的
比较器

class SimpleComparator implements Comparator<String[]> {
    @Override
    public int compare(String[] o1, String o2[]) {       
        if(!o1[0].equalsIgnoreCase(o2[0])){
            return o1[0].compareToIgnoreCase(o2[0]);
        }
        int value1 = Integer.parseInt(o1[2]);
        int value2 = Integer.parseInt(o2[2]);
        if(value1!=value2){
            return new Integer(value1).compareTo(value2);
        }
        return 0;
    }
}

您已经尝试过搜索答案,但实际上已经尝试过编写代码。这里有一个明显的模式,但是我不明白为什么你不会有一个
Employee
类。我有一个Employee类,我没有包含简单的代码谢谢!我已经尝试过好几次了,但都不起作用,但我想通过阅读您的代码我能更好地理解它。我知道我是一个新手,但如果我实现了Employee类的可比接口,这对我的项目的影响与将代码添加到我的Company类有什么不同?@lilkeblerelf1您可以创建一个Employee数组。这将更容易维护一个2字符串4维数组。实际上,使用字符串数据类型来表示年龄是没有意义的。
class SimpleComparator implements Comparator<String[]> {
    @Override
    public int compare(String[] o1, String o2[]) {       
        if(!o1[0].equalsIgnoreCase(o2[0])){
            return o1[0].compareToIgnoreCase(o2[0]);
        }
        int value1 = Integer.parseInt(o1[2]);
        int value2 = Integer.parseInt(o2[2]);
        if(value1!=value2){
            return new Integer(value1).compareTo(value2);
        }
        return 0;
    }
}
 Arrays.sort(departmentList,new SimpleComparator());
 for(int row = 0; row < departmentList.length; row++)
 {
      System.out.println(departmentList[row][0] + "\t" + departmentList[row][1] + "\t" + departmentList[row][2]);
 }
Accounting  Counting Novice 25
Accounting  Counting Savvy  40
Accounting  Counting Pro    45
Accounting  Counting Guru   55
Human Resources Hiring Pro  47
Human Resources Hiring Guru 58
Information Systems Hacking Novice  23
Information Systems Hacking Savvy   38
Information Systems Hacking Pro 46
Information Systems Hacking Guru    51
Marketing   Sales Savvy 38
Marketing   Sales Pro   48
Marketing   Sales Guru  50