0

I want to link and sort in descending order a two dimensional array. I coded my program in a way so that you can print the array but it is not sorted. How can I make it sorted?

This is a program to calculate the number of hours worked by 8 employees during the week (7 days), and print them out in descending order:

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

This is what I got in the console:

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

But I am supposed to get something like this:

Employee 7: 41
Employee 6: 37
Employee 0: 34
Employee 4: 32
Employee 3: 31
Employee 1: 28
Employee 5: 28
Employee 2: 20
APerson
  • 8,140
  • 8
  • 35
  • 49
Juan C Perez
  • 1
  • 1
  • 1
  • Do you want to sort the employees by _total_ time worked? – APerson Nov 05 '14 at 02:43
  • This should answer your question http://stackoverflow.com/questions/15452429/java-arrays-sort-2d-array In the compare function, use the sum of the arrays. – yts Nov 05 '14 at 02:51

3 Answers3

0

Try the Arrays.sort() method that takes a custom Comparator (source: 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]);
    }
});

If you don't feel like using the Java API, here's selection sort on a two-dimensional array:

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;
}
Community
  • 1
  • 1
APerson
  • 8,140
  • 8
  • 35
  • 49
0

I suggest you start with an Employee POJO to store the weekly hours and employee number like

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

Then you can implement your sumRow with a for-each and something like

public static double sumRow(double[] m) {
    double total = 0;
    for (double val : m) {
        total += val;
    }
    return total;
}

And finally, I think your main() could then use Arrays.sort(Object[]) like

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

And I get what I think is the correct output,

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
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

With a few changes to your code:

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;
}
SkyMaster
  • 1,323
  • 1
  • 8
  • 15