i'm trying to write a program that computes the totals from a
two-dimensional array then sorts by the largest total. however, I'm
loosing the original record when i do the sort, becuase i can't figure
out how to pass the index[key] when the swap takes place.
public class WeeklyHours {
public static void main(String[] args) {
//declare and define the array
int[][] employeeHours = {
{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}};
//display hours total hours worked by employee
int[] totals = sumHours(employeeHours);
int[] index = sort(totals);
//for (int i = 0; i <= index.length; i++)
// System.out.println(index[i]);
printArray(index, totals);
}
//define method which totals each row
public static int[] sumHours(int[][] array) {
//declare array and set values to 0
int[] total = new int[array.length];
//for loop used to tally array values
for (int i = 0; i < array.length; i++){
for (int j = 0; j < array[i].length; j++)
total[i] += array[i][j];
}
return total;
}
//define method to sort array in desending order
public static int[] sort(int[] array) {
//setup array to keep track of employees
int[] index = new int[array.length];
for (int i = 0; i < array.length; i++)
index[i] = i;
//start for loop from bottom of array
for (int i = array.length - 1; i >= 1; i--) {
int currentMin = array[0];
int currentMinIndex = 0;
for (int j = 1; j <= i; j++) {
if (currentMin > array[j]) {
currentMin = array[j];
currentMinIndex = j;
}
}
if (currentMinIndex != i) {
array[currentMinIndex] = array[i];
array[i] = currentMin;
}
}
return index;
}
public static void printArray(int[] array1, int[] array2) {
for (int i = 0, j = 0; i < array1.length; i++, j++)
System.out.println("Employee" + array1[j] + ": " + array2[i] + "
hours");
}
}
I'd like this to output in this format:
employee7: 41
employee6: 37
employee5: 34
etc.
etc.
Any advice would be great, I think i'm close, just can't get the last
piece.
-sb
Roedy Green - 25 Apr 2006 04:49 GMT
> //define method to sort array in desending order
> public static int[] sort(int[] array) {
You can use a built in sort which you can trust . see
http://mindprod.com/jgloss/sort.html

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Bjorn Abelli - 25 Apr 2006 15:22 GMT
<bellsb@gmail.com> wrote...
> i'm trying to write a program that computes the totals from a
> two-dimensional array then sorts by the largest total. however,
> I'm loosing the original record when i do the sort, becuase
> i can't figure out how to pass the index[key] when the swap
> takes place.
There are several solution to this, so I'll give you the two that comes up
on the top of my head.
1 Swap the elements of the array index at
the same time as you make the swap in totals.
...
for (int i = array.length - 1; i >= 1; i--)
{
int currentMin = array[0];
int currentMinIndex = 0;
int currentIndex = 0; // <-
for (int j = 1; j <= i; j++)
{
if (currentMin > array[j])
{
currentMin = array[j];
currentMinIndex = j;
currentIndex = index[j]; // <-
}
}
if (currentMinIndex != i)
{
array[currentMinIndex] = array[i];
array[i] = currentMin;
index[currentIndex] = index[i]; // <-
index[i] = currentIndex; // <-
}
}
...
2 [My preferred solution]
Encapsulate the employees working hours as properties
of an Employee class. Create an array (or another
collection) of Employees which you sort by the
total hours they've made...
// Bjorn A