如何以降序显示二维数组

时间:2014-11-05 02:38:51

标签: java sorting multidimensional-array

我想按降序链接和排序二维数组。我以某种方式对程序进行编码,以便您可以打印数组,但它没有排序。我怎样才能对其进行排序?

这是一个计算8名员工在一周(7天)内工作的小时数的计划,并按降序打印出来:

public class WeeklyHours {
    public static void main(String[] args) {
        double[][] employeeWorkHours = { 
                { 2, 4, 3, 4, 5, 8, 8 },
                { 7, 3, 4, 3, 3, 4, 4 }, 
                { 3, 3, 4, 3, 3, 2, 2 },
                { 9, 3, 4, 7, 3, 4, 1 }, 
                { 3, 5, 4, 3, 6, 3, 8 },
                { 3, 4, 4, 6, 3, 4, 4 }, 
                { 3, 7, 4, 8, 3, 8, 4 },
                { 6, 3, 5, 9, 2, 7, 9 } };

        for (int row = 0; row < employeeWorkHours.length; row++)
            System.out.println("Employee " + row + " : "
                    + sumRow(employeeWorkHours, row));
    }

    public static double sumRow(double[][] m, int rowIndex) {
        double total = 0;

        for (int col = 0; col < m[0].length; col++) {
            total += m[rowIndex][col];
        }

        return total;
    }
}

这是我在控制台中得到的:

Employee 0 : 34.0
Employee 1 : 28.0
Employee 2 : 20.0
Employee 3 : 31.0
Employee 4 : 32.0
Employee 5 : 28.0
Employee 6 : 37.0
Employee 7 : 41.0

但我应该得到这样的东西:

Employee 7: 41
Employee 6: 37
Employee 0: 34
Employee 4: 32
Employee 3: 31
Employee 1: 28
Employee 5: 28
Employee 2: 20

3 个答案:

答案 0 :(得分:0)

尝试使用自定义比较器的Arrays.sort()方法(来源:this answer):

java.util.Arrays.sort(employeeWorkHours, new java.util.Comparator<double[]>() {
    public int compare(double[] a, double[] b) {
        return Double.compare(a[0], b[0]);
    }
});

如果您不想使用Java API,请在二维数组上使用selection sort

int numEmployees = employeeWorkHours.length; // for convenience

// Looping through each of the employees
for(int employee = 0; employee < numEmployees; employee++) {

    // Find the employee who's worked the most out of the remaining ones
    int maxTimeEmployee = employee; // Start with the current one
    for(int i = employee; i < numEmployees; i++) {
        if(sumRow(employeeWorkHours, i) > sumRow(employeeWorkHours, maxTimeEmployee)) {

            // We've found a new maximum
            maxTimeEmployee = i;
        }
    }

    // Swap the current employee with the maximum one
    double[] tempHours = employeeWorkHours[employee];
    employeeWorkHours[employee] = employeeWorkHours[maxTimeEmployee];
    employeeWorkHours[maxTimeEmployee] = tempHours;
}

答案 1 :(得分:0)

我建议您从Employee POJO开始,以存储每周营业时间和员工编号,例如

static class Employee implements Comparable<Employee> {
    int num;
    double hours;

    public Employee(int num, double hours) {
        this.num = num;
        this.hours = hours;
    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    public double getHours() {
        return hours;
    }

    public void setHours(double hours) {
        this.hours = hours;
    }

    @Override
    public String toString() {
        return String.format("Employee #%d - Hours %.1f", num, hours);
    }

    @Override
    public int compareTo(Employee o) {
        int c = Double.valueOf(this.hours).compareTo(o.hours);
        if (c != 0) {
            return -c;
        }
        return Integer.valueOf(this.num).compareTo(o.num);
    }
}

然后,您可以使用for-each

之类的方式实施sumRow
public static double sumRow(double[] m) {
    double total = 0;
    for (double val : m) {
        total += val;
    }
    return total;
}

最后,我认为您的main()可以使用Arrays.sort(Object[])之类的

public static void main(String[] args) {
    double[][] employeeWorkHours = { { 2, 4, 3, 4, 5, 8, 8 },
            { 7, 3, 4, 3, 3, 4, 4 }, { 3, 3, 4, 3, 3, 2, 2 },
            { 9, 3, 4, 7, 3, 4, 1 }, { 3, 5, 4, 3, 6, 3, 8 },
            { 3, 4, 4, 6, 3, 4, 4 }, { 3, 7, 4, 8, 3, 8, 4 },
            { 6, 3, 5, 9, 2, 7, 9 } };
    Employee[] totals = new Employee[employeeWorkHours.length];
    for (int i = 0; i < employeeWorkHours.length; i++) {
        totals[i] = new Employee(i, sumRow(employeeWorkHours[i]));
    }
    Arrays.sort(totals);
    for (Employee e : totals) {
        System.out.println(e);
    }
}

我得到了我认为正确的输出,

Employee #7 - Hours 41.0
Employee #6 - Hours 37.0
Employee #0 - Hours 34.0
Employee #4 - Hours 32.0
Employee #3 - Hours 31.0
Employee #1 - Hours 28.0
Employee #5 - Hours 28.0
Employee #2 - Hours 20.0

答案 2 :(得分:0)

对代码进行一些更改:

public class WeeklyHours  {
public static void main(String[] args) {
    double[][] employeeWorkHours = { 
            { 2, 4, 3, 4, 5, 8, 8 },
            { 7, 3, 4, 3, 3, 4, 4 }, 
            { 3, 3, 4, 3, 3, 2, 2 },
            { 9, 3, 4, 7, 3, 4, 1 }, 
            { 3, 5, 4, 3, 6, 3, 8 },
            { 3, 4, 4, 6, 3, 4, 4 }, 
            { 3, 7, 4, 8, 3, 8, 4 },
            { 6, 3, 5, 9, 2, 7, 9 } };

    int len=employeeWorkHours.length;      
    double[][] employeeTotal=new double [len][2];

    for (int row = 0; row < len; row++) {
        employeeTotal[row][0]=row;
        employeeTotal[row][1]=sumRow(employeeWorkHours, row);                    
        System.out.println("Employee " + (int)employeeTotal[row][0] + 
                " : " + employeeTotal[row][1]);            
    }
    System.out.println("\nOrder by Hours:");
    Arrays.sort(employeeTotal, new java.util.Comparator<double[]>() {
        public int compare(double[] a, double[] b) {
        return Double.compare(b[1], a[1]);
        }
    });

    for (int row = 0; row < len; row++) 
        System.out.println("Employee " + (int) employeeTotal[row][0] +
                " : " + employeeTotal[row][1]);
}        

public static double sumRow(double[][] m, int rowIndex) {
    double total = 0;
    for (int col = 0; col < m[0].length; col++) {
        total += m[rowIndex][col];
    }
    return total;
}