Bubble sorting my array does not sort it

Posted by Trixmix on Game Development See other posts from Game Development or by Trixmix
Published on 2012-08-31T22:53:40Z Indexed on 2012/09/01 3:50 UTC
Read the original article Hit count: 147

Filed under:

I sort an array of chunks by doing this:

    for (int i =0; i<this.getQueue().size();i++) {
        for (int j =0; j<this.getQueue().size()-i-1;j++) {
            Chunk temp1 = this.getQueue().get(i);
            Chunk temp2 = this.getQueue().get(i+1);
                if (temp1 != null &&temp2 != null && temp2.getLocation().getY() < temp1.getLocation().getY()) {
                    this.getQueue().set(i, temp2);
                    this.getQueue().set(i+1, temp1);
                }
            }
        }

What I want is the the chunks with the lowest Y coordinate will be at the start of the array and the ones with the bigger Y coordinate will be at the end of the array. And this is my result:

1024.0
944.0
1104.0
944.0
1104.0
----BEFORE-----
944.0
1024.0
944.0
1104.0
1104.0
---AFTER---

Why is this not working It seams fine. I dont want to use a comparator so dont suggest it.

More info, the Y cords are floats. I got the result by for each looping on this queue and printed the Y locations.

© Game Development or respective owner

Related posts about java