repost. i guess i seemed needy.

How to sort an array in ascending order

Posted 15 November 2009 - 05:37 PM

Well im doing an arrays project and well these are the instructions "The elevators in our building have an 1100 lb. load limit. Determine which people in the list above get on the elevator. Print their names, weights, total weight, and how many got on." its an array of people and their weights my problem is when i run it the weight goes over the max weight which is 1100... my question is can anyone hint me on what i can do to fix this??? thanks very much!!! here is my code so far

Re: How to sort an array in ascending order

Posted 15 November 2009 - 05:47 PM

that was amazing advice thank you

now my next task is to do this

Rearrange these people in ascending sequence by weight and print the two arrays. Determine again how many may ride the elevator, printing out their names, weights, total weight and the number of how many people got on.

Rearrange these people in ascending sequence by name (USE A DIFFERENT SORT ALGORITHM THAN THE ONE YOU USED ) and print the two arrays. Determine again how many may ride the elevator, printing out their names, weights, total weight and the number of how many people got on.

Have the program determine which method allowed the most people to get on the elevator.

any advice. again i do not want anyone to do this for me.
i need advice.

Re: How to sort an array in ascending order

Posted 15 November 2009 - 05:50 PM

lbaby, on 15 Nov, 2009 - 04:47 PM, said:

that was amazing advice thank you

now my next task is to do this

Rearrange these people in ascending sequence by weight and print the two arrays. Determine again how many may ride the elevator, printing out their names, weights, total weight and the number of how many people got on.

Rearrange these people in ascending sequence by name (USE A DIFFERENT SORT ALGORITHM THAN THE ONE YOU USED ) and print the two arrays. Determine again how many may ride the elevator, printing out their names, weights, total weight and the number of how many people got on.

Have the program determine which method allowed the most people to get on the elevator.

any advice. again i do not want anyone to do this for me.
i need advice.

Re: How to sort an array in ascending order

Posted 16 November 2009 - 04:24 PM

First, you cannot declare local variables (any variable declared in a method) as public, private, protected or static. Second, if you are trying to use parallel arrays to correlate people to their respective weights, then understand that using Arrays.sort() on each array will destroy this order.

If you want to maintain the order, then I suggest creating your own Person class containing the attributes name and weight. Make sure your class Person implements the Comparable interface if you want Arrays.sort() to work. Like so:

Then all you have to do is use an array of Persons (Person[]) and call the Arrays.sort() method on it. And because you implement Comparable in your Person class, Arrays.sort() knows to compare the Persons based on their weights only.