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