我的代码用于对员工列表进行排序,首先按部门排序(第一列),然后按年龄(第三列)按升序排序。我搜索了几个小时无济于事。到目前为止我的代码:
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]);
}
}
}
我希望输出根据部门打印列表,然后根据年龄,最小到最旧。任何帮助表示赞赏。
答案 0 :(得分:0)
您可以使用Arrays.sort
并提供自定义比较器:
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]);
}
});
实际上,更好的方法是创建自己的Employee
类,并让它实现类似的界面。
输出:
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
答案 1 :(得分:0)
这样做
您的Comparator
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