0

I have absolutely no idea how to go about this, but I'm using an array for an AI Pathfinding system in Java and I need to sort the values. Here is the Array I'm generating:

int[][] paths = new int[Settings.GAME_COLS][Settings.GAME_ROWS]; // Just creating an Array that has room for every tile in the Game, so the Array can be used like this: paths[x][y]
int tempF = 0;

tempF = Math.abs(14 + (((aiX + 1) - playerX) + ((aiY - 1) - playerY)));
paths[aiX + 1][aiY - 1] = tempF;
tempF = Math.abs(14 + (((aiX + 1) - playerX) + ((aiY + 1) - playerY)));
paths[aiX + 1][aiY + 1] = tempF;
tempF = Math.abs(14 + (((aiX - 1) - playerX) + ((aiY + 1) - playerY)));
paths[aiX - 1][aiY + 1] = tempF;
tempF = Math.abs(14 + (((aiX - 1) - playerX) + ((aiY - 1) - playerY)));
paths[aiX - 1][aiY - 1] = tempF;
tempF = Math.abs(10 + (((aiX + 1) - playerX) + (aiY - playerY)));
paths[aiX + 1][aiY    ] = tempF;
tempF = Math.abs(10 + (((aiX - 1) - playerX) + (aiY - playerY)));
paths[aiX - 1][aiY    ] = tempF;
tempF = Math.abs(10 + ((aiX - playerX) + ((aiY + 1) - playerY)));
paths[aiX    ][aiY + 1] = tempF;
tempF = Math.abs(10 + ((aiX - playerX) + ((aiY - 1) - playerY)));
paths[aiX    ][aiY - 1] = tempF;

It all works perfectly find and generates the Array with the information needed, but I need to sort the array based on the "tempF" value added in to the array. Is there an easy way to do this?

1
  • I put a 2D sort could you try that too? Please, and could tell me the result? Commented Sep 4, 2012 at 19:06

2 Answers 2

2

A multi-dimensional array is nothing more than a set of arrays, so you can use regular Arrays.sort method which will sort your array of arrays in-place.

Sign up to request clarification or add additional context in comments.

4 Comments

I have tried that already - I just get a lot of Console Errors. Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: [I cannot be cast to java.lang.Comparable
This is all I'm using, Arrays.sort(paths);
@ThomasMosey You can't use Arrays.sort on the 2-dimensional array, you have to call it on the 1-dimensional arrays in paths.
you will need to do something like for (int[] path : paths) { Arrays.sort(path); }
0
for(int k=0;k<N;k++)  // loop for relaxation between row-column sorts
{
    for(int i=0;i<N;i++)
    {
       //row-wise sortings(for all rows)
       Arrays.sort(your_array[i]); 
    }

      for ( int c = 0 ; c < N ; c++ )
      {
         for( int d = 0 ; d < N ; d++ )               
         transposed_your_array[d][c] = your_array[c][d];         
      }  

    for(int i=0;i<N;i++)
    {
       //column-wise sortings(for all columns)
       Arrays.sort(transposed_your_array[i]);
    }

    for ( int c = 0 ; c < N ; c++ )
    {
        for( int d = 0 ; d < N ; d++ )               
        your_array[d][c] = transposed_your_array[c][d];         
    }  //getting original array from transpose


}
//There are actually N*N 1-D(N-element)arrays in a N x N matrix
//Every sort on a row-array alters the bounding column-arrays too(N of them)


//a worked example:
//8  3  8
//11 8  3
//6  12 6

//output:
//  this(0,0) is smallest element
//  ^
//  |
//  3  6  8
//  3  8  11
//  6  8  12 ---->this (2,2) is biggest element

//some other examples:
//10 8 8 
//11 4 7 
//9 5 3 
//
//3 5 9 ----> this 9 could be in place of 8 down there but this 
//4 7 10      program sorts rows first then sorts columns
//8 8 11      you can interleave them for better resolution sort(slower)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.