# How to sort an array in ascending order

Page 1 of 1

## 11 Replies - 5400 Views - Last Post: 16 November 2009 - 04:24 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=139304&amp;s=b4bc5f311d343489d6db67d502197857&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 lbaby

Reputation: 0
• Posts: 11
• Joined: 27-October 09

# 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
```import java.util.Scanner;

public class Arrays
{
public static void main( String [] args)
{
int [] weight = {30,150,305,225,135,160,80,200,165,90,100,120};
String [] people = {"Anne","Bob ","Ralph","Tim ","Barbara","Jane","Steve","Tom ","Mike","Shirley","Pam ","Frank"};
for (int i=0;i<weight.length;i++)
System.out.println(people[i] + "\t\t" + weight[i]);

int i = 0;
int tweight= 0;
while(tweight <1100)
{
tweight += weight[i];
i++;
}
System.out.println();
System.out.println( "Number of people: " + i + " With total weight: " + tweight);
}
}

```

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.

what is some advice to make that happen.

all im printing is the total weight.
im guessing my brain is freezing up from trying to figure this out.
i dont want anyone to do it for me.

Is This A Good Question/Topic? 0

## Replies To: How to sort an array in ascending order

### #2 erik.price

• D.I.C Lover

Reputation: 486
• Posts: 2,690
• Joined: 18-December 08

## Re: How to sort an array in ascending order

Posted 15 November 2009 - 05:43 PM

You should change this segment:
```while(tweight <1100)
{
tweight += weight[i];
i++;
}
```

to something more like this:

```while((tweight + weight[i])<1100)
{
tweight += weight[i];
i++;

}

```

That should guarantee that the 1,100lb weight limit isn't exceeded

Edit: woops typo

This post has been edited by erik.price: 15 November 2009 - 05:46 PM

### #3 jimdandy75

• D.I.C Regular

Reputation: 37
• Posts: 311
• Joined: 30-June 08

## Re: How to sort an array in ascending order

Posted 15 November 2009 - 05:44 PM

It's your while statement, let's say the total weight is 1097, it will run the while statement again.
Thus taking tweight over 1100.

Yeah, what he ^ said, beat me to it by a second.

This post has been edited by jimdandy75: 15 November 2009 - 05:46 PM

### #4 lbaby

Reputation: 0
• Posts: 11
• Joined: 27-October 09

## 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.

### #5 jimdandy75

• D.I.C Regular

Reputation: 37
• Posts: 311
• Joined: 30-June 08

## 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.

Oh crap, now it gets hard !!!!! I'm out, gotta watch the Colts game. Good Luck

### #6 lbaby

Reputation: 0
• Posts: 11
• Joined: 27-October 09

## Re: How to sort an array in ascending order

Posted 15 November 2009 - 06:00 PM

helllpppp

helllpppp

### #7 lbaby

Reputation: 0
• Posts: 11
• Joined: 27-October 09

## Re: How to sort an array in ascending order

Posted 15 November 2009 - 07:19 PM

```import java.util.Scanner;

public class Elevator
{
public static void main( String [] args)
{
int [] weight = {30,150,305,225,135,160,80,200,165,90,100,120};
String [] people = {"Anne","Bob ","Ralph","Tim ","Barbara","Jane","Steve","Tom ","Mike","Shirley","Pam ","Frank"};
for (int i=0;i<weight.length;i++)
System.out.println(people[i] + "\t\t" + weight[i]);

int i = 0;
int tweight= 0;
while((tweight + weight[i++])<1100)
{
tweight += weight[i];

}

System.out.println();
System.out.println( "Number of people: " + i + " With total weight: " + tweight);

}
}

```

how do you sort this in ascending order

### #8 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: How to sort an array in ascending order

Posted 15 November 2009 - 07:47 PM

The Cone Snippet is full of sorts of all sorts

### #9 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: How to sort an array in ascending order

Posted 15 November 2009 - 08:14 PM

Topics merged
You have 4 posts with almost the same question

### #10 chaos386

Reputation: 5
• Posts: 28
• Joined: 27-February 09

## Re: How to sort an array in ascending order

Posted 15 November 2009 - 08:20 PM

this may help i dont realy under stand what you are asking for but this with sort by waits

```import java.util.Scanner;

public class Elevator
{
//holds the people and there waits in one object
private People[] peps = new People[12];
//this is your stuff i'm just lazy
private int[] weight = {30,150,305,225,135,160,80,200,165,90,100,120};
private String[] people = {"Anne","Bob ","Ralph","Tim ","Barbara","Jane","Steve","Tom ","Mike","Shirley","Pam ","Frank"};

//main methods this is so you dont run into static problems
public Elevator(){
//new stuff
//sets up the array
popArray();
//prints the array
printArray();
System.out.println();
//sorts the array / names
sortByNumber();
//prints it again
printArray();
System.out.println();

for (int i=0;i<weight.length;i++)
System.out.println(people[i] + "\t\t" + weight[i]);

int i = 0;
int tweight= 0;
while((tweight + weight[i++])<1100)
{
tweight += weight[i];

}

System.out.println();
System.out.println( "Number of people: " + i + " With total weight: " + tweight);

}
public void popArray(){
for(int i = 0; i<peps.length; i++){
peps[i] = new People(weight[i],people[i]);

}
}
public void sortByNumber(){
People temp;
People[] tempArray = new People[peps.length];
for(int i=0; i<peps.length; i++){
//temp holding place
temp = peps[i];

for(int j=i; j<peps.length; j++){
//comapiers
if(peps[j].weight<temp.weight){
//puts the smallest in the temp holding
peps[i] = peps[j];
peps[j] = temp;
temp = peps[i];

}

}
tempArray[i] = temp;
}
peps = tempArray;
}
public void printArray(){
for(int i= 0; i<peps.length;i++)
System.out.println(peps[i].name+"\t\t"+peps[i].weight);
}

public static void main( String [] args)
{
new Elevator();
}
}

```

and you make this node to work with it so you will need to comapile this file to make it run right
```public class People{
public int weight;
public String name;

public People(int w, String name){

weight = w;
this.name = name;
}

}

```

and its as esay as that o stated working on this when you first put it up so i miised out on the post after it hope it still helps

### #11 lbaby

Reputation: 0
• Posts: 11
• Joined: 27-October 09

## Re: How to sort an array in ascending order

Posted 16 November 2009 - 10:46 AM

Okay. I do need a main and a driver and apparently they now HATE EACH OTHER.

i tried to combine.
error

here are my two codes:

```import java.util.Arrays;

public class testElevator
{
public static void main( String [] args)
{
private int [] weight = {30,150,305,225,135,160,80,200,165,90,100,120};
private String [] names = {"Anne","Bob ","Ralph","Tim ","Barbara","Jane","Steve","Tom ","Mike","Shirley","Pam ","Frank"};
for (int i=0;i<weight.length;i++)
System.out.println(names[i] + "\t\t" + weight[i]);
System.out.println();

int i = 0;
int tweight= 0;
while((tweight + weight[i++])<1100)
{
tweight += weight[i];

}

System.out.println( "Number of people: " + i + " With total weight: " + tweight);
System.out.println(names[i] +"\t\t" + weight[i]);

Arrays.sort(names);
System.out.println();
System.out.println("In Ascending order by WEIGHT:");
System.out.println();

Arrays.sort(weight);
//System.out.println(Arrays.toString(weight));
for(int x=0; x<12; x++){

System.out.println(names[x] + "\t\t" + weight[x]);
}

Arrays.sort(weight);
System.out.println();
System.out.println("In Ascending order by Names:");
System.out.println();

Arrays.sort(weight);
//System.out.println(Arrays.toString(names));
for(int y=0; y<12; y++){

System.out.println(names[y] + "\t\t" + weight[y]);
}

}
}

```

```public class People{
public int weight;
public String name;

public People(int w, String name){

weight = w;
this.name = name;
}

}

```

This is what I get:

how do i make them work together

----jGRASP exec: javac -g C:\Users\LauraJane\Documents\testElevator.java

testElevator.java:9: illegal start of expression
private int [] weight = {30,150,305,225,135,160,80,200,165,90,100,120};
^
testElevator.java:10: illegal start of expression
private String [] names = {"Anne","Bob ","Ralph","Tim ","Barbara","Jane","Steve","Tom ","Mike","Shirley","Pam ","Frank"};
^
2 errors

----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.

ALSO...how do i get the load limit to print their names of who fits in the elevator.
it's right here in the code

```
int i = 0;
int tweight= 0;
while((tweight + weight[i++])<1100)
{
tweight += weight[i];

}

System.out.println( "Number of people: " + i + " With total weight: " + tweight);
System.out.println(names[i] +"\t\t" + weight[i]);

```

This post has been edited by lbaby: 16 November 2009 - 10:46 AM

### #12 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12271
• Posts: 45,363
• Joined: 27-December 08

## 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:
```public class Person implements Comparable{
private String name;
private double weight;

public Person(String n, double w){
name = n;
weight = w;
}

public String getName(){return name;}
public double getWeight(){return weight;}

public int compareTo(Person p){
if(weight > p.getWeight()) return 1;
else if(weight == p.getWeight()) return 0;
return -1;
}
}

```

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.