programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

# Sorting arrays while retaining original index

Greenhorn
Posts: 1
• Number of slices to send:
Optional 'thank-you' note:
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?

Ranch Hand
Posts: 385
• Number of slices to send:
Optional 'thank-you' note:
Negative number indicates the insertion point when the search for the particular value is not found.

Marshal
Posts: 79277
377
• Number of slices to send:
Optional 'thank-you' note:
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.

Campbell Ritchie
Marshal
Posts: 79277
377
• Number of slices to send:
Optional 'thank-you' note:
And welcome to JavaRanch