Forums Register Login

Sorting arrays while retaining original index

+Pie Number of slices to send: Send
Hey there everyone, first post here so please excuse any mistakes

Ok so I have this assignment here:

Suppose the weekly hours for all employees are stored in a two dimensional array. Each row records an employee's seven-day works hours with seven columns. For example, the following array stores the work hours for eight employees. Write a program that displays employees and their total hours in decreasing order of the total hours.
Su Mon Tue Wed Thu Fri Sa
Emp 0 2 4 3 4 5 8 8
Emp 1 7 3 4 3 3 4 4
Emp 2 3 3 4 3 3 2 2
Emp 3 9 3 4 7 3 4 1
Emp 4 3 5 4 3 6 3 8
Emp 5 3 4 4 6 3 4 4
Emp 6 3 7 4 8 3 8 4
Emp 7 6 3 5 9 2 7 9

The final output is supposed to look 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

Ok... so what i have done so far is create the 2D array and then made a new loop to add across each row and store that value into a new array. that looks like this
[34 28 20 31 32 28 37 41]

And I used the reverseOrder method to sort it into descending order. I then use the binarySearch function to find out which employee had what hours... but it doesnt seem to work. Any inputs would be greatly appreciated!


Oh and my current output is this:

34
28
20
31
32
28
37
41

41
37
34
32
31
28
28
20
Employee 7 has this many hours: 41
Employee 6 has this many hours: 37
Employee -7 has this many hours: 34
Employee -7 has this many hours: 32

Employee 3 has this many hours: 31
Employee 1 has this many hours: 28
Employee 1 has this many hours: 28
Employee -1 has this many hours: 20


I dont get why the negative numbers are appearing?? and how do i get it to read employee 5 and not just stop at employee 1?
+Pie Number of slices to send: Send
Negative number indicates the insertion point when the search for the particular value is not found.
+Pie Number of slices to send: Send
There is no such thing as a 2D array in Java; only an array of arrays.

You should be doing it in an object-oriented fashion. Create an EmployeeHours class with name and hours worked as fields.
+Pie Number of slices to send: Send
And welcome to JavaRanch
Proudly marching to the beat of a different kettle of fish... while reading this tiny ad
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 2943 times.
Similar Threads
Something i'm extremely confused on
Magic Square water retention
summing each row in a 2-dimensional array
Two dimensional array total and sort
Convert Into Java Code.
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 07:49:56.