This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Sorting arrays while retaining original index Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Sorting arrays while retaining original index" Watch "Sorting arrays while retaining original index" New topic
Author

Sorting arrays while retaining original index

Q Tran
Greenhorn

Joined: Oct 22, 2009
Posts: 1
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?
Siva Masilamani
Ranch Hand

Joined: Sep 19, 2008
Posts: 385
Negative number indicates the insertion point when the search for the particular value is not found.


SCJP 6,SCWCD 5,SCBCD 5

Failure is not an option.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38033
    
  22
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
Sheriff

Joined: Oct 13, 2005
Posts: 38033
    
  22
And welcome to JavaRanch
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sorting arrays while retaining original index
 
Similar Threads
Convert Into Java Code.
summing each row in a 2-dimensional array
Two dimensional array total and sort
Something i'm extremely confused on
Magic Square water retention