This site requires JavaScript, please enable it in your browser!
Greenfoot back
Dylan1999
Dylan1999 wrote ...

2016/4/5

Help with sorting array of numbers?

Dylan1999 Dylan1999

2016/4/5

#
public void display()
    {
        GreenfootImage image = getImage();
        image.clear();
        image.setColor(Color.BLACK);
        image.drawString("The List of " + numbers.length+" Numbers", 40, 50);

        for (int i=1; i<10; i++)
        {
            int x = 40;
            int y = 80;
            for (int j=1; j<10; j++)
            {
                image.drawString(numbers[i*j] + "  ", x*i, y);
                y=y+35;

            }
        }
    }
Above is my code to just sort numbers in a 10x10 square. I would like to sort them in an ascending order.
danpost danpost

2016/4/5

#
Actually, your code creates a 9x9 square of numbers (the loops iterate from one to nine); and there is really no 'sorting' going on there. How is the 'numbers' array filled (what code are you using to assign values to the elements of the 'numbers' array)?
Dylan1999 Dylan1999

2016/4/6

#
public void createArray() { for(int i = 0; i<numbers.length; i++) { numbers = Greenfoot.getRandomNumber(99)+1; } display(); } Sorry, I said 'sort' for lack of a better word. Programming isn't exactly my forte, I'm still trying to learn the basics.
danpost danpost

2016/4/6

#
The following line should sort the array:
java.util.Collections.sort(java.util.Arrays.asList(numbers));
You need to login to post a reply.