Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm quite new to Java, but getting into it. However, I can't wrap my head around why this example, which I found here, does not work:

At start of class:

String[][] spritesPaint = new String[20][20];

In method:

for (int funct1 = 0; funct1 <= 2; funct1++) {
    if (funct1 == 0) {
        for (int funct2 = 0; funct2 < rEnemyNumber; funct2++) {
            spritesPaint[0][funct2] = new Integer(rEnemyY[funct2])
                    .toString();
            spritesPaint[1][funct2] = rEnemyGraphic[funct2];
        }
    } else if (funct1 == 1) {
        Arrays.sort(Integer.valueOf(spritesPaint[0].toString()),
                new Comparator<Integer[]>() {
                    @Override
                    public int compare(final Integer[] entry1,
                            final Integer[] entry2) {
                        final Integer time1 = entry1[0];
                        final Integer time2 = entry2[0];
                        return time1.compareTo(time2);
                    }
                });

    } else if (funct1 == 2) {
        for (int funct3 = 0; funct3 < rEnemyNumber; funct3++) {
            if (rEnemyCheck[funct3] == true) {
                nextPaint = getImage(base, rEnemyGraphic[funct3]);
                System.out.println("Next: " + nextPaint);

                g.drawImage(nextPaint, rEnemyX[funct3] + worldCenterX,
                        rEnemyY[funct3] + worldCenterY, this);
            }
        }
    }
}

Basically, what I want to do is have a two dimensional array where I store Y position of object on the screen and an image path also related to that object, then sort it by the Y position integer. This should allow me to paint elements to the screen in the correct order for an isometric perspective.

However, I keep getting this error:

The method sort(T[], Comparator<? super T>) in the type Arrays 
is not applicable for the arguments (Integer, new Comparator<Integer[]>(){})

Please help me, I've been twisting my brain for hours trying to understand why I get this error now.

share|improve this question
    
The first paramater of Arrays.sort() must be an array. You are passing an Integer. –  jlordo Jan 16 '13 at 20:18
2  
@BevynQ: No, Arrays.sort() only takes an array as it's first parameter. –  jlordo Jan 16 '13 at 20:19

2 Answers 2

Integer.valueOf(spritesPaint[0].toString()) is a single integer, however from your description it seems that you want to sort the strings in the 1-dimensional array spritesPaint[0] as if they were integers. Here is a modification that will do just that:

Arrays.sort(spritesPaint[0], new Comparator<String>() {
    @Override public int compare(final String entry1, final String entry2) {
        final Integer time1 = Integer.valueOf(entry1);
        final Integer time2 = Integer.valueOf(entry2);
        return time1.compareTo(time2);
    }
});

Alternatively, if you are trying to sort the first dimension of the array by the first element of the second dimension of the array, modify as follows:

Arrays.sort(spritesPaint, new Comparator<String[]>() {
    @Override public int compare(final String[] entry1, final String[] entry2) {
        final Integer time1 = Integer.valueOf(entry1[0]);
        final Integer time2 = Integer.valueOf(entry2[0]);
        return time1.compareTo(time2);
    }
});
share|improve this answer
    
This made me go, "ahhh!" as to why it didn't run. And now it does run, except it does not really sort the Y values as I am hoping for. Currently there are four values, and they now output like this: Y: 450 Y: 600 Y: 550 Y: 500 ... could there be something else that I'm doing wrong? –  Magnus Jan 16 '13 at 21:04
    
Ah, no, think I figured it out. My bad! :) –  Magnus Jan 16 '13 at 21:15

The error message means that, instead of passing an Array of type T (generic), you are passing an Integer. So, where you have:

Arrays.sort(Integer.valueOf(spritesPaint[0].toString()), ...

you want to pass in

Arrays.sort(arrayToBeSorted, ...

(Also, your variables could do with better names because I really don't understand what this example is doing...)

share|improve this answer
    
Yes, that does make sense, thank you! –  Magnus Jan 16 '13 at 21:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.